Here are all of the changes that Python 2.4 makes to the core Python language.
>>> 'www.python.org'.split('.', 1) ['www', 'python.org'] 'www.python.org'.rsplit('.', 1) ['www.python', 'org']
cmp is the same as the previous single argument to sort(); if provided, the value should be a comparison function that takes two arguments and returns -1, 0, or +1 depending on how the arguments compare.
key should be a single-argument function that takes a list element and returns a comparison key for the element. The list is then sorted using the comparison keys. The following example sorts a list case-insensitively:
>>> L = ['A', 'b', 'c', 'D'] >>> L.sort() # Case-sensitive sort >>> L ['A', 'D', 'b', 'c'] >>> L.sort(key=lambda x: x.lower()) >>> L ['A', 'b', 'c', 'D'] >>> L.sort(cmp=lambda x,y: cmp(x.lower(), y.lower())) >>> L ['A', 'b', 'c', 'D']
The last example, which uses the cmp parameter, is the old way to perform a case-insensitive sort. It works but is slower than using a key parameter. Using key results in calling the lower() method once for each element in the list while using cmp will call it twice for each comparison.
For simple key functions and comparison functions, it is often possible to avoid a lambda expression by using an unbound method instead. For example, the above case-insensitive sort is best coded as:
>>> L.sort(key=str.lower) >>> L ['A', 'b', 'c', 'D']
The reverse parameter should have a Boolean value. If the value
is True, the list will be sorted into reverse order.
Instead of L.sort(lambda x,y: cmp(x.score, y.score)) ;
L.reverse()
, you can now write: L.sort(key = lambda x: x.score,
reverse=True)
.
The results of sorting are now guaranteed to be stable. This means that two entries with equal keys will be returned in the same order as they were input. For example, you can sort a list of people by name, and then sort the list by age, resulting in a list sorted by age where people with the same age are in name-sorted order.
>>> L = [9,7,8,3,2,4,1,6,5] >>> [10+i for i in sorted(L)] # usable in a list comprehension [11, 12, 13, 14, 15, 16, 17, 18, 19] >>> L # original is left unchanged [9,7,8,3,2,4,1,6,5] >>> sorted('Monty Python') # any iterable may be an input [' ', 'M', 'P', 'h', 'n', 'n', 'o', 'o', 't', 't', 'y', 'y'] >>> # List the contents of a dict sorted by key values >>> colormap = dict(red=1, blue=2, green=3, black=4, yellow=5) >>> for k, v in sorted(colormap.iteritems()): ... print k, v ... black 4 blue 2 green 3 red 1 yellow 5
>>> def transpose(array): ... return zip(*array) ... >>> transpose([(1,2,3), (4,5,6)]) [(1, 4), (2, 5), (3, 6)] >>> transpose([]) []
sys.modules
. The
incomplete module object left behind would fool further imports of the
same module into succeeding, leading to confusing errors.
LIST_APPEND
, that simplifies
the generated bytecode for list comprehensions and speeds them up
by about a third.
s = s +
"abc"
and s += "abc"
are now performed more efficiently in
certain circumstances. This optimization won't be present in other
Python implementations such as Jython, so you shouldn't rely on it;
using the join() method of strings is still recommended when
you want to efficiently glue a large number of strings together.
The net result of the 2.4 optimizations is that Python 2.4 runs the pystone benchmark around XX% faster than Python 2.3 and YY% faster than Python 2.2.
See About this document... for information on suggesting changes.