What's new in Python 3

Backports

Python 2.6's whole purpose in life is to make the move to version 3 as easy as possible, so many Python 3.0 features were backported to Python 2.6.

Resource management using with is an important new feature in Python 2.6. A resource (file, socket, mutex, …) is automatically bound when entering and released when exiting the with block. This idiom will remind C++ programmers of "Resource Acquisition Is Initialization" (RAII), wherein a resource is bound to an object. The with statement acts like try … finally from the user's point of view, in that both the try block and the finally block are always executed. All of this happens without explicit exception handling.

How does all of this work? In a with block, you can use any object provided by the context management protocol – that is, which has internal __enter()__ and __exit()__ methods. When entering the with block, the __enter()__ method is automatically called, as is __exit()__ when exiting the block.

The file object comes complete with these methods (Listing 2). Resource management is easy to code, however. A classic application would be to protect a code block against simultaneous access. Listing 3 has placeholders for the code block. The locked class objects prevent competitive access in the with block by using myLock to synchronize the block.

Listing 2

With Block with File

 

Listing 3

Protecting a Code Block

 

If this is too much work for you, you can use the contextmanager decorator from the new contextlib [2] library to benefit from its resource management functionality. Many other applications are listed in the Python Enhancement Proposal (PEP) 0343 [3].

Abstractions

The biggest syntactic extension to Python 2.6 is probably the introduction of abstract base classes. Whether an object could be used in a context previously depended on the object's properties and not on its formal interface specification. This idiom is known as duck typing, from a James Whitcomb Riley poem ("When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.").

If a class contains an abstract method, it becomes an abstract base class and cannot be instantiated. Derivative classes can only be created if they implement these abstract methods. Abstract base classes in Python act in a similar way to abstract base classes in C++, in that abstract methods are specifically allowed to contain an implementation.

Besides abstract methods, Python also uses abstract properties. Python uses abstract base classes in the numbers [4] and collections [5] modules. That still leaves the question: When does a class become an abstract class? The class needs the ABCMeta metaclass. The methods can then be declared as @abstractmethod, and the properties as @abstactproperty using the corresponding decorators. In addition to this, the use of abstract base classes means the entry of static typing into a language that uses dynamic typing. In the example in Figure 2, the Cygnus class cannot be instantiated because it does not implement the abstract quack() method.

Multiple Processors

Python's response to multi-processor architectures is the new multiprocessing [6] library. This module imitates the well-known Python threading module, but instead of a thread, it creates a process, and it does this independently of the platform. The multiprocessing module became necessary because CPython, the standard Python implementation, could only run one thread in its interpreter. This behavior is dictated by the Global Interpreter Lock (GIL) [7].

Class decorators [8] round out the list in Python; from now on, you can decorate classes as well as functions.

Python 3.0 also has a new I/O library [9]. The string data type gained a new format() [10] method for improved string formatting. At the same time, the previous format operator, %, is deprecated in Python 3.1.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Optimizing Python

    The trick to optimization is to save time in the right places.

  • Python match

    Exploring the new Python match statement, Python's implementation of switch/case.

  • Python generators simulate gambling

    Can 10 heads in a row really occur in a coin toss? Or, can the lucky numbers in the lottery be 1, 2, 3, 4, 5, 6? We investigate the law of large numbers.

  • Practical Python in Linux

    We’ll introduce you to Python, an easy-to-learn scripting language, and also help you get started creating your own practical Python scripts.

  • Analytics with Python and KDD

    The Knowledge Discovery in Data Mining (KDD) method breaks the business of data analytics into easy-to-understand steps. We'll show you how to get started with KDD and Python.

comments powered by Disqus
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.

Learn More

News