Section A.1. Major Changes in 2.0
This section lists changes introduced in Python release 2.0. Note that third-party extensions built for Python 1.5.x or 1.6 cannot be used with Python 2.0; these extensions must be rebuilt for 2.0. Python bytecode files (*.pyc and *.pyo) are not compatible between releases either.
A.1.1 Core Language Changes
The following sections describe changes made to the Python language itself.
A.1.1.1 Augmented assignment
After nearly a decade of complaints from C programmers, Guido broke down and added 11 new C-like assignment operators to the language:
+= -= *= /= %= **= <<= >>= &= ^= |=
The statement A += B is similar to A = A + B except that A is evaluated only once (useful if it is a complex expression). If A is a mutable object, it may be modified in place; for instance, if it is a list, A += B has the same effect as A.extend(B).
Classes and built-in object types can override the new operators in order to implement the in-place behavior; the non-in-place behavior is automatically used as a fallback when an object does not implement the in-place behavior. For classes, the method name is the method name for the corresponding non-in-place operator prepended with an "i" (e.g., __iadd__ implements in-place __add__ ).
A.1.1.2 List comprehensions
A new expression notation was added for lists whose elements are computed from another list (or lists):
[ For example, [i**2 for
i in
range(4)] yields the list
[0,1,4,9]. This is more efficient than using
map with a lambda, and at least in the context of
scanning lists, avoids some scoping issues raised by lambdas (e.g.,
using defaults to pass in information from the enclosing scope). You
can also add a condition:
[ For example, [w for w in words if w == w.lower( )]
yields the list of words that contain no uppercase characters. This
is more efficient than filter with a lambda.
Nested for loops and more than one
if is supported as well, though using this seems
to yield code that is as complex as nested maps and lambdas (see
Python manuals for more details).
Import
statements now allow an "as" clause (e.g.,
import mod
as name), which saves an
assignment of an imported modules name to another variable.
This works with from statements and package paths
too (e.g., from mod
import var as
name. The word "as" was not made a
reserved word in the process. (To import odd filenames that
don map to Python variable names, see the __import_
_ built-in function.)
The print
statement now has an option that makes the output go to a different
file than the default sys.stdout. For instance, to
write an error message to sys.stderr, you can now
write:
print >> sys.stderr, "spam"
As a special case, if the expression used to indicate the file
evaluates to None, the current value of
sys.stdout is used (like not using
>> at all). Note that you can always write
to file objects such as sys.stderr by calling
their write method; this optional extension simply
adds the extra formatting performed by the print statement (e.g.,
string conversion, spaces between items).
Python is now equipped with a garbage
collector that can hunt down cyclical references between Python
objects. It does not replace reference counting (and in fact depends
on the reference counts being correct), but can decide that a set of
objects belongs to a cycle if all their reference counts are
accounted for in their references to each other. A new module named
gc lets you control parameters of the garbage
collection; an option to the Python "configure" script
lets you enable or disable the garbage collection. (See the 2.0
release notes or the library manual to check if this feature is
enabled by default or not; because running this extra garbage
collection step periodically adds performance overheads, the decision
on whether to turn it on by default is pending.)
This is a partial list of standard library
changes introduced by Python release 2.0; see 2.0 release notes for a
full description of the changes.
A new function zip was added:
zip(seq1,seq2,...) is equivalent to
map(None,seq1,seq2,...) when the sequences have
the same length. For instance, zip([1,
2, 3], [10,
20, 30]) returns
[(1,10), (2,20),
(3,30)]. When the lists are not all the same
length, the shortest list defines the results length.
A new standard module named
pyexpat provides an interface to the Expat XML
parser. A new standard module package named xml
provides assorted XML support code in (so far) three subpackages:
xml.dom , xml.sax , and
xml.parsers.
The new
webbrowser module attempts to provide a
platform-independent API to launch a web browser. (See also the
LaunchBrowser script at the end of Chapter 4.)
Portability was ensured to 64-bit
platforms under both Linux and Win64, especially for the new Intel
Itanium processor. Large file support was also added for Linux64 and
Win64.
The
garbage collection changes resulted in the creation of two new slots
on an object, tp_traverse and
tp_clear. The augmented assignment changes result
in the creation of a new slot for each in-place operator. The GC API
creates new requirements for container types implemented in C
extension modules. See Include/objimpl.h in the
Python source distribution.
New popen2,
popen3, and popen4 calls were
added in the os module.
The os.popen call is now much more usable on
Windows 95 and 98. To fix this call for Windows 9x, Python internally
uses the w9xpopen.exe program in the root of your
Python installation (it is not a standalone program). See Microsoft
Knowledge Base article Q150956 for more details.
Administrator privileges are no longer required to install Python on
Windows NT or Windows 2000. The Windows installer also now installs
by default in Python20 on the default volume
(e.g., C:Python20 ), instead of the older-style
Program
FilesPython-2.0.
The Windows installer no longer runs a
separate Tcl/Tk installer; instead, it installs the needed Tcl/Tk
files directly in the Python directory. If you already have a Tcl/Tk
installation, this wastes some disk space (about 4 MB) but avoids
problems with conflicting Tcl/Tk installations and makes it much
easier for Python to ensure that Tcl/Tk can find all its
files.
A.1.1.3 Extended import statements
A.1.1.4 Extended print statement
A.1.1.5 Optional collection of cyclical garbage
A.1.2 Selected Library Changes
A.1.2.1 New zip function
A.1.2.2 XML support
A.1.2.3 New web browser module
A.1.3 Python/C Integration API Changes
A.1.4 Windows Changes
Категории