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 in ]

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 in if ]

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).

A.1.1.3 Extended import statements

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.)

A.1.1.4 Extended print statement

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).

A.1.1.5 Optional collection of cyclical garbage

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.)

A.1.2 Selected Library Changes

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.1.2.1 New zip function

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.1.2.2 XML support

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.

A.1.2.3 New web browser module

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.)

A.1.3 Python/C Integration API Changes

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.

A.1.4 Windows Changes

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.

Python 2 1 Alpha Features

Like the weather in Colorado, if you wait long enough, Pythons feature set changes. Just before this edition went to the printer, the first alpha release of Python 2.1 was announced. Among its new weapons are these:

  • Functions can now have arbitrary attributes attached to them; simply assign to function attribute names to associate extra information with the function (something coders had been doing with formatted documentation stings).
  • A new rich comparison extension now allows classes to overload individual comparison operators with distinct methods (e.g., __lt__ overloads < tests), instead of trying to handle all tests in the single __cmp__ method.
  • A warning framework provides an interface to messages issued for use of deprecated features (e.g., the regex module).
  • The Python build system has been revamped to use the Distutils package.
  • A new sys.displayhook attribute allows users to customize the way objects are printed at the interactive prompt.
  • Line-by-line file input/output (the file readline method) was made much faster, and a new xreadlines file method reads just one line at a time in for loops.
  • Also: the numeric coercion model used in C extensions was altered, modules may now set an __all__ name to specify which names they export for from * imports, the ftplib module now defaults to "passive" mode to work better with firewalls, and so on.
  • Other enhancements, such as statically nested scopes and weak references, were still on the drawing board in the alpha release.

As usual, of course, you should consult this books web page (http://www.rmi.net/~lutz/about-pp.html) and Python 2.1 and later release notes for Python developments that will surely occur immediately after I ship this insert off to my publisher.

Категории