Here are all of the changes that Python 2.4 makes to the core Python language.
from module import names
statement (PEP 328).
>>> 'www.python.org'.split('.', 1) ['www', 'python.org'] 'www.python.org'.rsplit('.', 1) ['www.python', 'org']
For the cmp parameter, the value should be a comparison function that takes two parameters and returns -1, 0, or +1 depending on how the parameters compare. This function will then be used to sort the list. Previously this was the only parameter that could be provided to sort().
key should be a single-parameter 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'] >>> # Using 'key' parameter to sort list >>> L.sort(key=lambda x: x.lower()) >>> L ['A', 'b', 'c', 'D'] >>> # Old-fashioned way >>> 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 calls lower() method once for each element in the list while using cmp will call it twice for each comparison, so using key saves on invocations of the lower() method.
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 written as:
>>> L.sort(key=str.lower) >>> L ['A', 'b', 'c', 'D']
Finally, the reverse parameter takes a Boolean value. If the
value is true, the list will be sorted into reverse order.
Instead of L.sort() ; L.reverse()
, you can now write
L.sort(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.
(All changes to sort() contributed by Raymond Hettinger.)
>>> 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
(Contributed by Raymond Hettinger.)
sys.path
,
and runs the module as a script. For example,
you can now run the Python profiler with python -m profile
.
(Contributed by Nick Coghlan.)
>>> 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. (Contributed by Raymond Hettinger.)
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.
(Contributed by Armin Rigo.)
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.