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
The find(S) method is often used to get an index which is then used to slice the string and obtain the pieces that are before and after the separator. 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') >>> ('file:/usr/share/doc/index.html').partition('://') ('file:/usr/share/doc/index.html', '', '') >>> (u'Subject: a quick question').partition(':') (u'Subject', u':', u' a quick question') >>> 'www.python.org'.rpartition('.') ('www.python', '.', 'org') >>> '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.)
id(self)
in
__hash__() methods (though this is discouraged).
# -*- coding: latin1 -*-
>>> chr(128) == unichr(128) # Can't convert chr(128) to Unicode __main__:1: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal False >>> chr(127) == unichr(127) # chr(127) can be converted True
Previously this would raise a UnicodeDecodeError exception,
but in 2.5 this could result in puzzling problems when accessing a
dictionary. If you looked up unichr(128)
and chr(128)
was being used as a key, you'd get a UnicodeDecodeError
exception. Other changes in 2.5 resulted in this exception being
raised instead of suppressed by the code in dictobject.c that
implements dictionaries.
Raising an exception for such a comparison is strictly correct, but the change might have broken code, so instead UnicodeWarning was introduced.
(Implemented by Marc-André Lemburg.)
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.)
The Python executable now accepts the standard long options --help and --version; on Windows, it also accepts the /? option for displaying a help message. (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
. (Proposed and implemented
by Raymond Hettinger.)
Frame objects are also slightly smaller, which may improve cache locality and reduce memory usage a bit. (Contributed by Neal Norwitz.)
See About this document... for information on suggesting changes.