What's new in Python 3
Clean-Up Work
Wherever changes occur, it is also necessary to ditch some ballast. This affects libraries that have been removed, that have been repackaged, or that coexist in C and Python implementations. The popular Python idiom (Listing 4) of importing the fast C implementation of a module first and then falling back on the Python implementation if this fails is no longer necessary. Python does this automatically. More details are available on changes to the standard library [11].
Listing 4
Import Idiom
All exceptions must derive from BaseException. This implies, in particular, that string exceptions are no longer supported. The exception object now has a new __traceback__ attribute, which contains the traceback of the exception. The approach to both calling and fielding exceptions with arguments has changed. Programmers can now throw exceptions with arguments using raise BaseException(args) and field them with except BaseException as variable (Figure 3).
Python 3 also includes other changes to make life easier for programmers. For example, in cooperative super calls, it is no longer necessary to name the class instance and the class name. Old-style classes, which were deprecated at some previous time, no longer exist in Python 3.0; this removes the need to derive from object to use Python's newer features.
Direct evaluation of input via the input() command is no longer supported, as the input is available as an input string. This approach closes a critical security hole (Figure 4). It was only logical to rename the raw_input() function input() and to remove raw_input.
Of course, any description of the new features can't hope to be exclusive. If you want to know more, check out the reference document by von Rossum, "What's New In Python 3.0" [12].
Porting to Python 3.0
A clear migration path is available for porting Python 2 code to Python 3 (Figure 5), but you will need to test the code and fix any bugs at each step of the way.
The four lines of code in Listing 5 will serve as an example for migrating Python 2 to Python 3.0. All four lines defined functional components of Python. The first function calculates the sum of three numbers, 2, 3, and 4, by applying these arguments to the Lambda function. The reduce built-in successively reduces the list of all numbers from 1 to 10 by multiplying the results of the last multiplication with the next number in the sequence. The last two functions filter words, starting with filtering uppercase letters out of a string.
Listing 5
Code for Port
The code works on Python 2.6, and you only need to perform Steps 3 and 4 for the port. The source code for this example is stored in a file called port.py.
Calling the Python 2.6 interpreter with the -3 option (Figure 6) shows incompatibilities with version 3: Both the apply function and the reduce function are no longer built-ins in Python 3.0. The code is easily fixed (Listing 6), and the deprecation warnings then stop.
Listing 6
Removing the Deprecation Warning
The code generator 2to3 is really useful if you need to correct your Python 2 code; the generator's final step is to automatically generate code for versions 3.0 and 3.1. The tool offers several options for this (Figure 7). The direct approach is to overwrite the original file: 2to3 port.py -w. The result is the ported source code for Python 3.0 (Listing 7).
Listing 7
Code Ported to Python 3.0
When to Make the Move
Python 3.0 originally placed more emphasis on functionality, and this meant that it was about 10 percent slower than Python 2. The required optimization occurred in Python 3.1 [13]. This optimization relates to special handling of small integers. On top of this, Python 3.1's I/O library is implemented in C, which makes it between 2 and 20 times faster. Decoding of the UTF-8, UTF-16, and Latin-1 character sets is now twice to four times as fast.
If you are still waiting for third-party libraries to be ported, there is no point porting your application code to Python 3. von Rossum also recommends [14] not writing any code that will run on both Python 2.6 and Python 3 without modifications. It is preferable to maintain the source code as Python 2.6 code and then use automated tools to port to Python 3.0 or 3.1. Christopher Neugebauer has the final word in his video talk on Python 3000: "Learn 2.6, but keep 3k in mind."
Infos
- Peters, Tim. The Zen of Python: http://www.python.org/dev/peps/pep-0020
- contextlib library: http://docs.python.org/3.0/library/contextlib.html#module-contextlib
- PEP 0343: http://www.python.org/dev/peps/pep-0343
- numbers library: http://docs.python.org//3.0/library/numbers.html#module-numbers
- collections library: http://docs.python.org/3.0/library/collections.html#module-collection
- multiprocessing module: http://docs.python.org/3.0/library/multiprocessing.html#module-multiprocessing
- GIL: http://docs.python.org/c-api/init.html#thread-state-and-the-global-interpreter-lock
- Class decorators: http://www.python.org/dev/peps/pep-3129
- io library: http://docs.python.org/3.0/library/io.html#module-io
- Format string method: http://docs.python.org/3.0/whatsnew/2.6.html#pep-3101
- Changes to libraries: http://docs.python.org/3.0/whatsnew/3.0.html#library-changes
- What's New in v.3.0: http://docs.python.org/3.0/whatsnew/3.0.html
- Optimizations in v.3.1: http://docs.python.org/dev/py3k/whatsnew/3.1.html#optimizations
- Porting to v.3.0: http://docs.python.org/3.0/whatsnew/3.0.html#miscellaneous-other-changes
« Previous 1 2 3
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.