| Author: | A.M. Kuchling |
|---|---|
| Release: | 2.6b1 |
| Date: | July 01, 2008 |
This article explains the new features in Python 2.6. The release schedule is described in PEP 361; currently the final release is scheduled for September 3 2008.
This article doesn’t attempt to provide a complete specification of the new features, but instead provides a convenient overview. For full details, you should refer to the documentation for Python 2.6. If you want to understand the rationale for the design and implementation, refer to the PEP for a particular new feature. Whenever possible, “What’s New in Python” links to the bug/patch item for each change.
The development cycle for Python 2.6 also saw the release of the first alphas of Python 3.0, and the development of 3.0 has influenced a number of features in 2.6.
Python 3.0 is a far-ranging redesign of Python that breaks compatibility with the 2.x series. This means that existing Python code will need a certain amount of conversion in order to run on Python 3.0. However, not all the changes in 3.0 necessarily break compatibility. In cases where new features won’t cause existing code to break, they’ve been backported to 2.6 and are described in this document in the appropriate place. Some of the 3.0-derived features are:
A new command-line switch, -3, enables warnings about features that will be removed in Python 3.0. You can run code with this switch to see how much work will be necessary to port code to 3.0. The value of this switch is available to Python code as the boolean variable sys.py3kwarning, and to C extension code as Py_Py3kWarningFlag.
Python 3.0 adds several new built-in functions and change the semantics of some existing built-ins. Entirely new functions such as bin() have simply been added to Python 2.6, but existing built-ins haven’t been changed; instead, the future_builtins module has versions with the new 3.0 semantics. Code written to be compatible with 3.0 can do from future_builtins import hex, map as necessary.
See also
The 3xxx series of PEPs, which describes the development process for Python 3.0 and various features that have been accepted, rejected, or are still under consideration.
While 2.6 was being developed, the Python development process underwent two significant changes: the developer group switched from SourceForge’s issue tracker to a customized Roundup installation, and the documentation was converted from LaTeX to reStructuredText.
For a long time, the Python developers have been growing increasingly annoyed by SourceForge’s bug tracker. SourceForge’s hosted solution doesn’t permit much customization; for example, it wasn’t possible to customize the life cycle of issues.
The infrastructure committee of the Python Software Foundation therefore posted a call for issue trackers, asking volunteers to set up different products and import some of the bugs and patches from SourceForge. Four different trackers were examined: Atlassian’s Jira, Launchpad, Roundup, and Trac. The committee eventually settled on Jira and Roundup as the two candidates. Jira is a commercial product that offers a no-cost hosted instance to free-software projects; Roundup is an open-source project that requires volunteers to administer it and a server to host it.
After posting a call for volunteers, a new Roundup installation was set up at http://bugs.python.org. One installation of Roundup can host multiple trackers, and this server now also hosts issue trackers for Jython and for the Python web site. It will surely find other uses in the future. Where possible, this edition of “What’s New in Python” links to the bug/patch item for each change.
Hosting is kindly provided by Upfront Systems of Stellenbosch, South Africa. Martin von Loewis put a lot of effort into importing existing bugs and patches from SourceForge; his scripts for this import operation are at http://svn.python.org/view/tracker/importer/.
See also
Since the Python project’s inception around 1989, the documentation had been written using LaTeX. At that time, most documentation was printed out for later study, not viewed online. LaTeX was widely used because it provided attractive printed output while remaining straightforward to write, once the basic rules of the markup have been learned.
LaTeX is still used today for writing technical publications destined for printing, but the landscape for programming tools has shifted. We no longer print out reams of documentation; instead, we browse through it online and HTML has become the most important format to support. Unfortunately, converting LaTeX to HTML is fairly complicated, and Fred L. Drake Jr., the Python documentation editor for many years, spent a lot of time wrestling the conversion process into shape. Occasionally people would suggest converting the documentation into SGML or, later, XML, but performing a good conversion is a major task and no one pursued the task to completion.
During the 2.6 development cycle, Georg Brandl put a substantial effort into building a new toolchain for processing the documentation. The resulting package is called Sphinx, and is available from http://sphinx.pocoo.org/. The input format is reStructuredText, a markup commonly used in the Python community that supports custom extensions and directives. Sphinx concentrates on HTML output, producing attractively styled and modern HTML, though printed output is still supported through conversion to LaTeX. Sphinx is a standalone package that can be used in documenting other projects.
See also
The previous version, Python 2.5, added the ‘with‘ statement an optional feature, to be enabled by a from __future__ import with_statement directive. In 2.6 the statement no longer needs to be specially enabled; this means that with is now always a keyword. The rest of this section is a copy of the corresponding section from “What’s New in Python 2.5” document; if you read it back when Python 2.5 came out, you can skip the rest of this section.
The ‘with‘ statement clarifies code that previously would use try...finally blocks to ensure that clean-up code is executed. In this section, I’ll discuss the statement as it will commonly be used. In the next section, I’ll examine the implementation details and show how to write objects for use with this statement.
The ‘with‘ statement is a new control-flow structure whose basic structure is:
with expression [as variable]:
with-block
The expression is evaluated, and it should result in an object that supports the context management protocol (that is, has __enter__() and __exit__() methods.
The object’s __enter__() is called before with-block is executed and therefore can run set-up code. It also may return a value that is bound to the name variable, if given. (Note carefully that variable is not assigned the result of expression.)
After execution of the with-block is finished, the object’s __exit__() method is called, even if the block raised an exception, and can therefore run clean-up code.
Some standard Python objects now support the context management protocol and can be used with the ‘with‘ statement. File objects are one example:
with open('/etc/passwd', 'r') as f:
for line in f:
print line
... more processing code ...
After this statement has executed, the file object in f will have been automatically closed, even if the for loop raised an exception part- way through the block.
Note
In this case, f is the same object created by open(), because file.__enter__() returns self.
The threading module’s locks and condition variables also support the ‘with‘ statement:
lock = threading.Lock()
with lock:
# Critical section of code
...
The lock is acquired before the block is executed and always released once the block is complete.
The new localcontext() function in the decimal module makes it easy to save and restore the current decimal context, which encapsulates the desired precision and rounding characteristics for computations:
from decimal import Decimal, Context, localcontext
# Displays with default precision of 28 digits
v = Decimal('578')
print v.sqrt()
with localcontext(Context(prec=16)):
# All code in this block uses a precision of 16 digits.
# The original context is restored on exiting the block.
print v.sqrt()
Under the hood, the ‘with‘ statement is fairly complicated. Most people will only use ‘with‘ in company with existing objects and don’t need to know these details, so you can skip the rest of this section if you like. Authors of new objects will need to understand the details of the underlying implementation and should keep reading.
A high-level explanation of the context management protocol is:
Let’s think through an example. I won’t present detailed code but will only sketch the methods necessary for a database that supports transactions.
(For people unfamiliar with database terminology: a set of changes to the database are grouped into a transaction. Transactions can be either committed, meaning that all the changes are written into the database, or rolled back, meaning that the changes are all discarded and the database is unchanged. See any database textbook for more information.)
Let’s assume there’s an object representing a database connection. Our goal will be to let the user write code like this:
db_connection = DatabaseConnection()
with db_connection as cursor:
cursor.execute('insert into ...')
cursor.execute('delete from ...')
# ... more operations ...
The transaction should be committed if the code in the block runs flawlessly or rolled back if there’s an exception. Here’s the basic interface for DatabaseConnection that I’ll assume:
class DatabaseConnection:
# Database interface
def cursor(self):
"Returns a cursor object and starts a new transaction"
def commit(self):
"Commits current transaction"
def rollback(self):
"Rolls back current transaction"
The __enter__() method is pretty easy, having only to start a new transaction. For this application the resulting cursor object would be a useful result, so the method will return it. The user can then add as cursor to their ‘with‘ statement to bind the cursor to a variable name.
class DatabaseConnection:
...
def __enter__(self):
# Code to start a new transaction
cursor = self.cursor()
return cursor
The __exit__() method is the most complicated because it’s where most of the work has to be done. The method has to check if an exception occurred. If there was no exception, the transaction is committed. The transaction is rolled back if there was an exception.
In the code below, execution will just fall off the end of the function, returning the default value of None. None is false, so the exception will be re-raised automatically. If you wished, you could be more explicit and add a return statement at the marked location.
class DatabaseConnection:
...
def __exit__(self, type, value, tb):
if tb is None:
# No exception, so commit
self.commit()
else:
# Exception occurred, so rollback.
self.rollback()
# return False
The new contextlib module provides some functions and a decorator that are useful for writing objects for use with the ‘with‘ statement.
The decorator is called contextmanager(), and lets you write a single generator function instead of defining a new class. The generator should yield exactly one value. The code up to the yield will be executed as the __enter__() method, and the value yielded will be the method’s return value that will get bound to the variable in the ‘with‘ statement’s as clause, if any. The code after the yield will be executed in the __exit__() method. Any exception raised in the block will be raised by the yield statement.
Our database example from the previous section could be written using this decorator as:
from contextlib import contextmanager
@contextmanager
def db_transaction(connection):
cursor = connection.cursor()
try:
yield cursor
except:
connection.rollback()
raise
else:
connection.commit()
db = DatabaseConnection()
with db_transaction(db) as cursor:
...
The contextlib module also has a nested(mgr1, mgr2, ...)() function that combines a number of context managers so you don’t need to write nested ‘with‘ statements. In this example, the single ‘with‘ statement both starts a database transaction and acquires a thread lock:
lock = threading.Lock()
with nested (db_transaction(db), lock) as (cursor, locked):
...
Finally, the closing(object)() function returns object so that it can be bound to a variable, and calls object.close at the end of the block.
import urllib, sys
from contextlib import closing
with closing(urllib.urlopen('http://www.yahoo.com')) as f:
for line in f:
sys.stdout.write(line)
See also
The documentation for the contextlib module.
Python’s -m switch allows running a module as a script. When you ran a module that was located inside a package, relative imports didn’t work correctly.
The fix in Python 2.6 adds a __package__ attribute to modules. When present, relative imports will be relative to the value of this attribute instead of the __name__ attribute. PEP 302-style importers can then set __package__. The runpy module that implements the -m switch now does this, so relative imports can now be used in scripts running from inside a package.
When you run Python, the module search path sys.modules usually includes a directory whose path ends in "site-packages". This directory is intended to hold locally-installed packages available to all users on a machine or using a particular site installation.
Python 2.6 introduces a convention for user-specific site directories. The directory varies depending on the platform:
Within this directory, there will be version-specific subdirectories, such as lib/python2.6/site-packages on Unix/MacOS and Python26/site-packages on Windows.
If you don’t like the default directory, it can be overridden by an environment variable. PYTHONUSERBASE sets the root directory used for all Python versions supporting this feature. On Windows, the directory for application-specific data can be changed by setting the APPDATA environment variable. You can also modify the site.py file for your Python installation.
The feature can be disabled entirely by running Python with the -s option or setting the PYTHONNOUSERSITE environment variable.
See also
multiprocessing makes it easy to distribute work over multiple processes. Its API is similiar to that of threading. For example:
from multiprocessing import Process
def long_hard_task(n):
print n * 43
for i in range(10):
Process(target=long_hard_task, args=(i)).start()
will multiply the numbers between 0 and 10 times 43 and print out the result concurrently.
See also
In Python 3.0, the % operator is supplemented by a more powerful string formatting method, format(). Support for the str.format() method has been backported to Python 2.6.
In 2.6, both 8-bit and Unicode strings have a .format() method that treats the string as a template and takes the arguments to be formatted. The formatting template uses curly brackets ({, }) as special characters:
# Substitute positional argument 0 into the string.
"User ID: {0}".format("root") -> "User ID: root"
# Use the named keyword arguments
uid = 'root'
'User ID: {uid} Last seen: {last_login}'.format(uid='root',
last_login = '5 Mar 2008 07:20') ->
'User ID: root Last seen: 5 Mar 2008 07:20'
Curly brackets can be escaped by doubling them:
format("Empty dict: {{}}") -> "Empty dict: {}"
Field names can be integers indicating positional arguments, such as {0}, {1}, etc. or names of keyword arguments. You can also supply compound field names that read attributes or access dictionary keys:
import sys
'Platform: {0.platform}\nPython version: {0.version}'.format(sys) ->
'Platform: darwin\n
Python version: 2.6a1+ (trunk:61261M, Mar 5 2008, 20:29:41) \n
[GCC 4.0.1 (Apple Computer, Inc. build 5367)]'
import mimetypes
'Content-type: {0[.mp4]}'.format(mimetypes.types_map) ->
'Content-type: video/mp4'
Note that when using dictionary-style notation such as [.mp4], you don’t need to put any quotation marks around the string; it will look up the value using .mp4 as the key. Strings beginning with a number will be converted to an integer. You can’t write more complicated expressions inside a format string.
So far we’ve shown how to specify which field to substitute into the resulting string. The precise formatting used is also controllable by adding a colon followed by a format specifier. For example:
# Field 0: left justify, pad to 15 characters
# Field 1: right justify, pad to 6 characters
fmt = '{0:15} ${1:>6}'
fmt.format('Registration', 35) ->
'Registration $ 35'
fmt.format('Tutorial', 50) ->
'Tutorial $ 50'
fmt.format('Banquet', 125) ->
'Banquet $ 125'
Format specifiers can reference other fields through nesting:
fmt = '{0:{1}}'
fmt.format('Invoice #1234', 15) ->
'Invoice #1234 '
width = 35
fmt.format('Invoice #1234', width) ->
'Invoice #1234 '
The alignment of a field within the desired width can be specified:
| Character | Effect |
|---|---|
| < (default) | Left-align |
| > | Right-align |
| ^ | Center |
| = | (For numeric types only) Pad after the sign. |
Format specifiers can also include a presentation type, which controls how the value is formatted. For example, floating-point numbers can be formatted as a general number or in exponential notation:
>>> '{0:g}'.format(3.75)
'3.75'
>>> '{0:e}'.format(3.75)
'3.750000e+00'
A variety of presentation types are available. Consult the 2.6 documentation for a complete list; here’s a sample:
'b' - Binary. Outputs the number in base 2.
'c' - Character. Converts the integer to the corresponding
Unicode character before printing.
'd' - Decimal Integer. Outputs the number in base 10.
'o' - Octal format. Outputs the number in base 8.
'x' - Hex format. Outputs the number in base 16, using lower-
case letters for the digits above 9.
'e' - Exponent notation. Prints the number in scientific
notation using the letter 'e' to indicate the exponent.
'g' - General format. This prints the number as a fixed-point
number, unless the number is too large, in which case
it switches to 'e' exponent notation.
'n' - Number. This is the same as 'g' (for floats) or 'd' (for
integers), except that it uses the current locale setting to
insert the appropriate number separator characters.
'%' - Percentage. Multiplies the number by 100 and displays
in fixed ('f') format, followed by a percent sign.
Classes and types can define a __format__ method to control how they’re formatted. It receives a single argument, the format specifier:
def __format__(self, format_spec):
if isinstance(format_spec, unicode):
return unicode(str(self))
else:
return str(self)
There’s also a format() built-in that will format a single value. It calls the type’s __format__() method with the provided specifier:
>>> format(75.6564, '.2f')
'75.66'
See also
The print statement becomes the print() function in Python 3.0. Making print() a function makes it easier to change by doing ‘def print(...)’ or importing a new function from somewhere else.
Python 2.6 has a __future__ import that removes print as language syntax, letting you use the functional form instead. For example:
from __future__ import print_function
print('# of entries', len(dictionary), file=sys.stderr)
The signature of the new function is:
def print(*args, sep=' ', end='\n', file=None)
The parameters are:
- args: positional arguments whose values will be printed out.
- sep: the separator, which will be printed between arguments.
- end: the ending text, which will be printed after all of the arguments have been output.
- file: the file object to which the output will be sent.
See also
One error that Python programmers occasionally make is the following:
try:
...
except TypeError, ValueError:
...
The author is probably trying to catch both TypeError and ValueError exceptions, but this code actually does something different: it will catch TypeError and bind the resulting exception object to the local name "ValueError". The correct code would have specified a tuple:
try:
...
except (TypeError, ValueError):
...
This error is possible because the use of the comma here is ambiguous: does it indicate two different nodes in the parse tree, or a single node that’s a tuple.
Python 3.0 changes the syntax to make this unambiguous by replacing the comma with the word “as”. To catch an exception and store the exception object in the variable exc, you must write:
try:
...
except TypeError as exc:
...
Python 3.0 will only support the use of “as”, and therefore interprets the first example as catching two different exceptions. Python 2.6 supports both the comma and “as”, so existing code will continue to work.
See also
Python 3.0 adopts Unicode as the language’s fundamental string type and denotes 8-bit literals differently, either as b'string' or using a bytes constructor. For future compatibility, Python 2.6 adds bytes as a synonym for the str type, and it also supports the b'' notation.
There’s also a __future__ import that causes all string literals to become Unicode strings. This means that \u escape sequences can be used to include Unicode characters:
from __future__ import unicode_literals
s = ('\u751f\u3080\u304e\u3000\u751f\u3054'
'\u3081\u3000\u751f\u305f\u307e\u3054')
print len(s) # 12 Unicode characters
At the C level, Python 3.0 will rename the existing 8-bit string type, called PyStringObject in Python 2.x, to PyBytesObject. Python 2.6 uses #define to support using the names PyBytesObject, PyBytes_Check, PyBytes_FromStringAndSize, and all the other functions and macros used with strings.
Instances of the bytes type are immutable just as strings are. A new bytearray type stores a mutable sequence of bytes:
>>> bytearray([65, 66, 67])
bytearray(b'ABC')
>>> b = bytearray(u'\u21ef\u3244', 'utf-8')
>>> b
bytearray(b'\xe2\x87\xaf \xe3\x89\x84')
>>> b[0] = '\xe3'
>>> b
bytearray(b'\xe3\x87\xaf \xe3\x89\x84')
>>> unicode(str(b), 'utf-8')
u'\u31ef \u3244'
Byte arrays support most of the methods of string types, such as startswith()/endswith(), find()/rfind(), and some of the methods of lists, such as append(), pop(), and reverse().
>>> b = bytearray('ABC')
>>> b.append('d')
>>> b.append(ord('e'))
>>> b
bytearray(b'ABCde')
See also
Python’s built-in file objects support a number of methods, but file-like objects don’t necessarily support all of them. Objects that imitate files usually support read() and write(), but they may not support readline(). Python 3.0 introduces a layered I/O library in the io module that separates buffering and text-handling features from the fundamental read and write operations.
There are three levels of abstract base classes provided by the io module:
RawIOBase: defines raw I/O operations: read(), readinto(), write(), seek(), tell(), truncate(), and close(). Most of the methods of this class will often map to a single system call. There are also readable(), writable(), and seekable() methods for determining what operations a given object will allow.
Python 3.0 has concrete implementations of this class for files and sockets, but Python 2.6 hasn’t restructured its file and socket objects in this way.
BufferedIOBase: is an abstract base class that buffers data in memory to reduce the number of system calls used, making I/O processing more efficient. It supports all of the methods of RawIOBase, and adds a raw attribute holding the underlying raw object.
There are four concrete classes implementing this ABC: BufferedWriter and BufferedReader for objects that only support writing or reading and don’t support random access, BufferedRandom for objects that support the seek() method for random access, and BufferedRWPair for objects such as TTYs that have both read and write operations that act upon unconnected streams of data.
TextIOBase: Provides functions for reading and writing strings (remember, strings will be Unicode in Python 3.0), and supporting universal newlines. TextIOBase defines the readline() method and supports iteration upon objects.
There are two concrete implementations. TextIOWrapper wraps a buffered I/O object, supporting all of the methods for text I/O and adding a buffer attribute for access to the underlying object. StringIO simply buffers everything in memory without ever writing anything to disk.
(In current 2.6 alpha releases, io.StringIO is implemented in pure Python, so it’s pretty slow. You should therefore stick with the existing StringIO module or cStringIO for now. At some point Python 3.0’s io module will be rewritten into C for speed, and perhaps the C implementation will be backported to the 2.x releases.)
In Python 2.6, the underlying implementations haven’t been restructured to build on top of the io module’s classes. The module is being provided to make it easier to write code that’s forward-compatible with 3.0, and to save developers the effort of writing their own implementations of buffering and text I/O.
See also
The buffer protocol is a C-level API that lets Python types exchange pointers into their internal representations. A memory-mapped file can be viewed as a buffer of characters, for example, and this lets another module such as re treat memory-mapped files as a string of characters to be searched.
The primary users of the buffer protocol are numeric-processing packages such as NumPy, which can expose the internal representation of arrays so that callers can write data directly into an array instead of going through a slower API. This PEP updates the buffer protocol in light of experience from NumPy development, adding a number of new features such as indicating the shape of an array, locking memory .
The most important new C API function is PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags), which takes an object and a set of flags, and fills in the Py_buffer structure with information about the object’s memory representation. Objects can use this operation to lock memory in place while an external caller could be modifying the contents, so there’s a corresponding PyObject_ReleaseBuffer(PyObject *obj, Py_buffer *view) to indicate that the external caller is done.
The flags argument to PyObject_GetBuffer specifies constraints upon the memory returned. Some examples are:
- PyBUF_WRITABLE indicates that the memory must be writable.
- PyBUF_LOCK requests a read-only or exclusive lock on the memory.
- PyBUF_C_CONTIGUOUS and PyBUF_F_CONTIGUOUS requests a C-contiguous (last dimension varies the fastest) or Fortran-contiguous (first dimension varies the fastest) layout.
See also
Some object-oriented languages such as Java support interfaces: declarations that a class has a given set of methods or supports a given access protocol. Abstract Base Classes (or ABCs) are an equivalent feature for Python. The ABC support consists of an abc module containing a metaclass called ABCMeta, special handling of this metaclass by the isinstance() and issubclass() built-ins, and a collection of basic ABCs that the Python developers think will be widely useful.
Let’s say you have a particular class and wish to know whether it supports dictionary-style access. The phrase “dictionary-style” is vague, however. It probably means that accessing items with obj[1] works. Does it imply that setting items with obj[2] = value works? Or that the object will have keys(), values(), and items() methods? What about the iterative variants such as iterkeys()? copy() and update()? Iterating over the object with iter()?
Python 2.6 includes a number of different ABCs in the collections module. Iterable indicates that a class defines __iter__(), and Container means the class supports x in y expressions by defining a __contains__() method. The basic dictionary interface of getting items, setting items, and keys(), values(), and items(), is defined by the MutableMapping ABC.
You can derive your own classes from a particular ABC to indicate they support that ABC’s interface:
import collections
class Storage(collections.MutableMapping):
...
Alternatively, you could write the class without deriving from the desired ABC and instead register the class by calling the ABC’s register() method:
import collections
class Storage:
...
collections.MutableMapping.register(Storage)
For classes that you write, deriving from the ABC is probably clearer. The register() method is useful when you’ve written a new ABC that can describe an existing type or class, or if you want to declare that some third-party class implements an ABC. For example, if you defined a PrintableType ABC, it’s legal to do:
# Register Python’s types PrintableType.register(int) PrintableType.register(float) PrintableType.register(str)
Classes should obey the semantics specified by an ABC, but Python can’t check this; it’s up to the class author to understand the ABC’s requirements and to implement the code accordingly.
To check whether an object supports a particular interface, you can now write:
def func(d):
if not isinstance(d, collections.MutableMapping):
raise ValueError("Mapping object expected, not %r" % d)
(Don’t feel that you must now begin writing lots of checks as in the above example. Python has a strong tradition of duck-typing, where explicit type-checking isn’t done and code simply calls methods on an object, trusting that those methods will be there and raising an exception if they aren’t. Be judicious in checking for ABCs and only do it where it helps.)
You can write your own ABCs by using abc.ABCMeta as the metaclass in a class definition:
from abc import ABCMeta
class Drawable():
__metaclass__ = ABCMeta
def draw(self, x, y, scale=1.0):
pass
def draw_doubled(self, x, y):
self.draw(x, y, scale=2.0)
class Square(Drawable):
def draw(self, x, y, scale):
...
In the Drawable ABC above, the draw_doubled() method renders the object at twice its size and can be implemented in terms of other methods described in Drawable. Classes implementing this ABC therefore don’t need to provide their own implementation of draw_doubled(), though they can do so. An implementation of draw() is necessary, though; the ABC can’t provide a useful generic implementation. You can apply the @abstractmethod decorator to methods such as draw() that must be implemented; Python will then raise an exception for classes that don’t define the method:
class Drawable():
__metaclass__ = ABCMeta
@abstractmethod
def draw(self, x, y, scale):
pass
Note that the exception is only raised when you actually try to create an instance of a subclass without the method:
>>> s=Square()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class Square with abstract methods draw
>>>
Abstract data attributes can be declared using the @abstractproperty decorator:
@abstractproperty
def readonly(self):
return self._x
Subclasses must then define a readonly() property
See also
Python 3.0 changes the syntax for octal (base-8) integer literals, which are now prefixed by “0o” or “0O” instead of a leading zero, and adds support for binary (base-2) integer literals, signalled by a “0b” or “0B” prefix.
Python 2.6 doesn’t drop support for a leading 0 signalling an octal number, but it does add support for “0o” and “0b”:
>>> 0o21, 2*8 + 1
(17, 17)
>>> 0b101111
47
The oct() built-in still returns numbers prefixed with a leading zero, and a new bin() built-in returns the binary representation for a number:
>>> oct(42)
'052'
>>> bin(173)
'0b10101101'
The int() and long() built-ins will now accept the “0o” and “0b” prefixes when base-8 or base-2 are requested, or when the base argument is zero (meaning the base used is determined from the string):
>>> int ('0o52', 0)
42
>>> int('1101', 2)
13
>>> int('0b1101', 2)
13
>>> int('0b1101', 0)
13
See also
Decorators have been extended from functions to classes. It’s now legal to write:
@foo @bar class A: pass
This is equivalent to:
class A:
pass
A = foo(bar(A))
See also
In Python 3.0, several abstract base classes for numeric types, inspired by Scheme’s numeric tower, are being added. This change was backported to 2.6 as the numbers module.
The most general ABC is Number. It defines no operations at all, and only exists to allow checking if an object is a number by doing isinstance(obj, Number).
Numbers are further divided into Exact and Inexact. Exact numbers can represent values precisely and operations never round off the results or introduce tiny errors that may break the commutativity and associativity properties; inexact numbers may perform such rounding or introduce small errors. Integers, long integers, and rational numbers are exact, while floating-point and complex numbers are inexact.
Complex is a subclass of Number. Complex numbers can undergo the basic operations of addition, subtraction, multiplication, division, and exponentiation, and you can retrieve the real and imaginary parts and obtain a number’s conjugate. Python’s built-in complex type is an implementation of Complex.
Real further derives from Complex, and adds operations that only work on real numbers: floor(), trunc(), rounding, taking the remainder mod N, floor division, and comparisons.
Rational numbers derive from Real, have numerator and denominator properties, and can be converted to floats. Python 2.6 adds a simple rational-number class, Fraction, in the fractions module. (It’s called Fraction instead of Rational to avoid a name clash with numbers.Rational.)
Integral numbers derive from Rational, and can be shifted left and right with << and >>, combined using bitwise operations such as & and |, and can be used as array indexes and slice boundaries.
In Python 3.0, the PEP slightly redefines the existing built-ins round(), math.floor(), math.ceil(), and adds a new one, math.trunc(), that’s been backported to Python 2.6. math.trunc() rounds toward zero, returning the closest Integral that’s between the function’s argument and zero.
See also
Scheme’s numerical tower, from the Guile manual.
Scheme’s number datatypes from the R5RS Scheme specification.
To fill out the hierarchy of numeric types, a rational-number class is provided by the fractions module. Rational numbers store their values as a numerator and denominator forming a fraction, and can exactly represent numbers such as 2/3 that floating-point numbers can only approximate.
The Fraction constructor takes two Integral values that will be the numerator and denominator of the resulting fraction.
>>> from fractions import Fraction
>>> a = Fraction(2, 3)
>>> b = Fraction(2, 5)
>>> float(a), float(b)
(0.66666666666666663, 0.40000000000000002)
>>> a+b
Fraction(16, 15)
>>> a/b
Fraction(5, 3)
To help in converting floating-point numbers to rationals, the float type now has a as_integer_ratio() method that returns the numerator and denominator for a fraction that evaluates to the same floating-point value:
>>> (2.5) .as_integer_ratio()
(5, 2)
>>> (3.1415) .as_integer_ratio()
(7074029114692207L, 2251799813685248L)
>>> (1./3) .as_integer_ratio()
(6004799503160661L, 18014398509481984L)
Note that values that can only be approximated by floating-point numbers, such as 1./3, are not simplified to the number being approximated; the fraction attempts to match the floating-point value exactly.
The fractions module is based upon an implementation by Sjoerd Mullender that was in Python’s Demo/classes/ directory for a long time. This implementation was significantly updated by Jeffrey Yasskin.
Here are all of the changes that Python 2.6 makes to the core Python language.
The hasattr() function was catching and ignoring all errors, under the assumption that they meant a __getattr__() method was failing somewhere and the return value of hasattr() would therefore be False. This logic shouldn’t be applied to KeyboardInterrupt and SystemExit, however; Python 2.6 will no longer discard such exceptions when hasattr() encounters them. (Fixed by Benjamin Peterson; issue 2196.)
When calling a function using the ** syntax to provide keyword arguments, you are no longer required to use a Python dictionary; any mapping will now work:
>>> def f(**kw):
... print sorted(kw)
...
>>> ud=UserDict.UserDict()
>>> ud['a'] = 1
>>> ud['b'] = 'string'
>>> f(**ud)
['a', 'b']
(Contributed by Alexander Belopolsky; issue 1686487.)
A new built-in, next(*iterator*, [*default*]) returns the next item from the specified iterator. If the default argument is supplied, it will be returned if iterator has been exhausted; otherwise, the StopIteration exception will be raised. (issue 2719)
Tuples now have an index() method matching the list type’s index() method:
>>> t = (0,1,2,3,4)
>>> t.index(3)
3
The built-in types now have improved support for extended slicing syntax, where various combinations of (start, stop, step) are supplied. Previously, the support was partial and certain corner cases wouldn’t work. (Implemented by Thomas Wouters.)
Properties now have three attributes, getter, setter and deleter, that are useful shortcuts for adding or modifying a getter, setter or deleter function to an existing property. You would use them like this:
class C(object):
@property
def x(self):
return self._x
@x.setter
def x(self, value):
self._x = value
@x.deleter
def x(self):
del self._x
class D(C):
@C.x.getter
def x(self):
return self._x * 2
@x.setter
def x(self, value):
self._x = value / 2
Several methods of the built-in set types now accept multiple iterables: intersection(), intersection_update(), union(), update(), difference() and difference_update().
>>> s=set('1234567890')
>>> s.intersection('abc123', 'cdf246') # Intersection between all inputs
set(['2'])
>>> s.difference('246', '789')
set(['1', '0', '3', '5'])
(Contributed by Raymond Hettinger.)
A numerical nicety: when creating a complex number from two floats on systems that support signed zeros (-0 and +0), the complex() constructor will now preserve the sign of the zero. (Fixed by Mark T. Dickinson; issue 1507)
More floating-point features were also added. The float() function will now turn the string nan into an IEEE 754 Not A Number value, and +inf and -inf into positive or negative infinity. This works on any platform with IEEE 754 semantics. (Contributed by Christian Heimes; issue 1635.)
Other functions in the math module, isinf() and isnan(), return true if their floating-point argument is infinite or Not A Number. (issue 1640)
The math module has a number of new functions, and the existing functions have been improved to give more consistent behaviour across platforms, especially with respect to handling of floating-point exceptions and IEEE 754 special values. The new functions are:
There’s also a new trunc() built-in function as a result of the backport of PEP 3141’s type hierarchy for numbers.
The existing math functions have been modified to follow the recommendations of the C99 standard with respect to special values whenever possible. For example, sqrt(-1.) should now give a ValueError across (nearly) all platforms, while sqrt(float('NaN')) should return a NaN on all IEEE 754 platforms. Where Annex ‘F’ of the C99 standard recommends signaling ‘divide-by-zero’ or ‘invalid’, Python will raise ValueError. Where Annex ‘F’ of the C99 standard recommends signaling ‘overflow’, Python will raise OverflowError. (See issue 711019, issue 1640.)
(Contributed by Christian Heimes and Mark Dickinson.)
Changes to the Exception interface as dictated by PEP 352 continue to be made. For 2.6, the message attribute is being deprecated in favor of the args attribute.
The GeneratorExit exception now subclasses BaseException instead of Exception. This means that an exception handler that does except Exception: will not inadvertently catch GeneratorExit. (Contributed by Chad Austin; issue 1537.)
Generator objects now have a gi_code attribute that refers to the original code object backing the generator. (Contributed by Collin Winter; issue 1473257.)
The compile() built-in function now accepts keyword arguments as well as positional parameters. (Contributed by Thomas Wouters; issue 1444529.)
The complex() constructor now accepts strings containing parenthesized complex numbers, letting complex(repr(cmplx)) will now round-trip values. For example, complex('(3+4j)') now returns the value (3+4j). (issue 1491866)
The string translate() method now accepts None as the translation table parameter, which is treated as the identity transformation. This makes it easier to carry out operations that only delete characters. (Contributed by Bengt Richter; issue 1193128.)
The built-in dir() function now checks for a __dir__() method on the objects it receives. This method must return a list of strings containing the names of valid attributes for the object, and lets the object control the value that dir() produces. Objects that have __getattr__() or __getattribute__() methods can use this to advertise pseudo-attributes they will honor. (issue 1591665)
Instance method objects have new attributes for the object and function comprising the method; the new synonym for im_self is __self__, and im_func is also available as __func__. The old names are still supported in Python 2.6; they’re gone in 3.0.
An obscure change: when you use the the locals() function inside a class statement, the resulting dictionary no longer returns free variables. (Free variables, in this case, are variables referred to in the class statement that aren’t attributes of the class.)
The net result of the 2.6 optimizations is that Python 2.6 runs the pystone benchmark around XX% faster than Python 2.5.
Two command-line options have been reserved for use by other Python implementations. The -J switch has been reserved for use by Jython for Jython-specific options, such as ones that are passed to the underlying JVM. -X has been reserved for options specific to a particular implementation of Python such as CPython, Jython, or IronPython. If either option is used with Python 2.6, the interpreter will report that the option isn’t currently used.
It’s now possible to prevent Python from writing .pyc or .pyo files on importing a module by supplying the -B switch to the Python interpreter, or by setting the PYTHONDONTWRITEBYTECODE environment variable before running the interpreter. This setting is available to Python programs as the sys.dont_write_bytecode variable, and can be changed by Python code to modify the interpreter’s behaviour. (Contributed by Neal Norwitz and Georg Brandl.)
The encoding used for standard input, output, and standard error can be specified by setting the PYTHONIOENCODING environment variable before running the interpreter. The value should be a string in the form **encoding** or **encoding**:**errorhandler**. The encoding part specifies the encoding’s name, e.g. utf-8 or latin-1; the optional errorhandler part specifies what to do with characters that can’t be handled by the encoding, and should be one of “error”, “ignore”, or “replace”. (Contributed by Martin von Loewis.)
As usual, Python’s standard library received a number of enhancements and bug fixes. Here’s a partial list of the most notable changes, sorted alphabetically by module name. Consult the Misc/NEWS file in the source tree for a more complete list of changes, or look through the Subversion logs for all the details.
(3.0-warning mode) Python 3.0 will feature a reorganized standard library; many outdated modules are being dropped. Python 2.6 running in 3.0-warning mode will warn about these modules when they are imported.
The list of deprecated modules is: audiodev, bgenlocations, buildtools, bundlebuilder, Canvas, compiler, dircache, dl, fpformat, gensuitemodule, ihooks, imageop, imgfile, linuxaudiodev, mhlib, mimetools,