This appendix summarizes prominent changes introduced in Python releases since the first edition of this book. It is divided into three sections, mostly because the sections on 1.6 and 2.0 changes were adapted from release note documents:
Changes introduced in Python 2.0 (and 2.1)
Changes introduced in Python 1.6
Changes between the first edition and Python 1.5.2
Python 1.3 was the most recent release when the first edition was published (October 1996), and Python 1.6 and 2.0 were released just before this second edition was finished. 1.6 was the last release posted by CNRI, and 2.0 was released from BeOpen (Guido’s two employers prior to his move to Digital Creations); 2.0 adds a handful of features to 1.6.
With a few notable exceptions, the changes over the last five years have introduced new features to Python, but have not changed it in incompatible ways. Many of the new features are widely useful (e.g., module packages), but some seem to address the whims of Python gurus (e.g., list comprehensions) and can be safely ignored by anyone else. In any event, although it is important to keep in touch with Python evolution, you should not take this appendix too seriously. Frankly, application library and tool usage is much more important in practice than obscure language additions.
For information on the Python changes that will surely occur after this edition’s publication, consult either the resources I maintain at this book’s web site (http://rmi.net/~lutz/about-pp.html), the resources available at Python’s web site (http://www.python.org ), or the release notes that accompany Python releases.
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.
The following sections describe changes made to the Python language itself.
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 new expression notation was added for lists whose elements are computed from another list (or lists):
[<expression> for <variable> in <sequence>]
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:
[<expression> for <variable> in <sequence> if <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 module’s 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’t 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 result’s 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.
18.226.159.33