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 CVS logs for all the details.
key
keyword argument similar to the one
provided by the min()/max() functions
and the sort() methods. For example:
Example:
>>> import heapq >>> L = ["short", 'medium', 'longest', 'longer still'] >>> heapq.nsmallest(2, L) # Return two lowest elements, lexicographically ['longer still', 'longest'] >>> heapq.nsmallest(2, L, key=len) # Return two shortest elements ['short', 'medium']
(Contributed by Raymond Hettinger.)
None
for the start and step arguments. This makes it more
compatible with the attributes of slice objects, so that you can now write
the following:
s = slice(5) # Create slice object itertools.islice(iterable, s.start, s.stop, s.step)
(Contributed by Raymond Hettinger.)
operator.attrgetter('a', 'b')
will return a function
that retrieves the a and b attributes. Combining
this new feature with the sort() method's key
parameter
lets you easily sort lists using multiple fields.
Constants named os.SEEK_SET, os.SEEK_CUR, and os.SEEK_END have been added; these are the parameters to the os.lseek() function. Two new constants for locking are os.O_SHLOCK and os.O_EXLOCK.
On FreeBSD, the os.stat() function now returns times with nanosecond resolution, and the returned object now has st_gen and st_birthtime. The st_flags member is also available, if the platform supports it.
A tarfile's compression can be autodetected by
using the mode 'r|*'
.
(Contributed by Lars Gustäbel.)
use_datetime=True
to the loads() function
or the Unmarshaller class to enable this feature.
See About this document... for information on suggesting changes.