Here are all of the changes that Python 2.5 makes to the core Python language.
class zerodict (dict): def __missing__ (self, key): return 0 d = zerodict({1:1, 2:2}) print d[1], d[2] # Prints 1, 2 print d[3], d[4] # Prints 0, 0
partition(sep) condenses this pattern into a single method call that returns a 3-tuple containing the substring before the separator, the separator itself, and the substring after the separator. If the separator isn't found, the first element of the tuple is the entire string and the other two elements are empty. rpartition(sep) also returns a 3-tuple but starts searching from the end of the string; the "r" stands for 'reverse'.
Some examples:
>>> ('http://www.python.org').partition('://') ('http', '://', 'www.python.org') >>> (u'Subject: a quick question').partition(':') (u'Subject', u':', u' a quick question') >>> ('file:/usr/share/doc/index.html').partition('://') ('file:/usr/share/doc/index.html', '', '') >>> 'www.python.org'.rpartition('.') ('www.python', '.', 'org')
(Implemented by Fredrik Lundh following a suggestion by Raymond Hettinger.)
def is_image_file (filename): return filename.endswith(('.gif', '.jpg', '.tiff'))
(Implemented by Georg Brandl following a suggestion by Tom Lynn.)
key
keyword parameter analogous to the key
argument for sort(). This parameter supplies a function that
takes a single argument and is called for every value in the list;
min()/max() will return the element with the
smallest/largest return value from this function.
For example, to find the longest string in a list, you can do:
L = ['medium', 'longest', 'short'] # Prints 'longest' print max(L, key=len) # Prints 'short', because lexicographically 'short' has the largest value print max(L)
(Contributed by Steven Bethard and Raymond Hettinger.)
# -*- coding: latin1 -*-
class C(): pass
In the interactive interpreter, quit
and exit
have long been strings so that new users get a somewhat helpful message
when they try to quit:
>>> quit 'Use Ctrl-D (i.e. EOF) to exit.'
In Python 2.5, quit
and exit
are now objects that still
produce string representations of themselves, but are also callable.
Newbies who try quit()
or exit()
will now exit the
interpreter as they expect. (Implemented by Georg Brandl.)
Several of the optimizations were developed at the NeedForSpeed sprint, an event held in Reykjavik, Iceland, from May 21-28 2006. The sprint focused on speed enhancements to the CPython implementation and was funded by EWT LLC with local support from CCP Games. Those optimizations added at this sprint are specially marked in the following list.
a = 2+3
, the code generator will do the arithmetic and produce
code corresponding to a = 5
.
Frame objects are also slightly smaller, which may improve cache locality and reduce memory usage a bit. (Contributed by Neal Norwitz.)
The net result of the 2.5 optimizations is that Python 2.5 runs the pystone benchmark around XXX% faster than Python 2.4.
See About this document... for information on suggesting changes.