Section A.3. Major Changes Between 1.3 and 1.5.2

This section describes significant language, library, tool, and C API changes in Python between the first edition of this book (Python 1.3) and Python release 1.5.2.

A.3.1 Core Language Changes

The following sections describe changes made to the Python language itself.

A.3.1.1 Pseudo-private class attributes

Python now provides a name-mangling protocol that hides attribute names used by classes. Inside a class statement, a name of the form _ _X is automatically changed by Python to _Class_ _X , where Class is the name of the class being defined by the statement. Because the enclosing class name is prepended, this feature limits the possibilities of name clashes when you extend or mix existing classes. Note that this is not a "private" mechanism at all, just a class name localization feature to minimize name clashes in hierarchies and the shared instance objects namespace at the bottom of the attribute inheritance links chain.

A.3.1.2 Class exceptions

Exceptions may now take the form of class (and class instance) objects. The intent is to support exception categories. Because an except clause will now match a raised exception if it names the raised class or any of its superclasses, specifying superclasses allows try statements to catch broad categories without listing all members explicitly (e.g., catching a numeric-error superclass exception will also catch specific kinds of numeric errors). Pythons standard built-in exceptions are now classes (instead of strings) and have been organized into a shallow class hierarchy; see the library manual for details.

A.3.1.3 Package imports

Import statements may now reference directory paths on your computer by dotted-path syntax. For instance:

import directory1.directory2.module # and use path from directory1.directory2.module import name # and use "name"

Both load a module nested two levels deep in packages (directories). The leftmost package name in an import path (directory1) must be a directory within a directory that is listed in the Python module search path (sys.path initialized from PYTHONPATH). Thereafter, the import statements path denotes subdirectories to follow. Paths prevent module name conflicts when installing multiple Python systems on the same machine that expect to find their own version of the same module name (otherwise, only the first on PYTHONPATH wins).

Unlike the older ni module that this feature replaces, the new package support is always available (without running special imports) and requires each package directory along an import path to contain a (possibly empty) __init__.py module file to identify the directory as a package, and serve as its namespace if imported directly. Packages tend to work better with from than with import, since the full path must be repeated to use imported objects after an import.

A.3.1.4 New assert statement

Python 1.5 added a new statement:

assert test [, value]

which is the same as:

if __debug__: if not test: raise AssertionError, value

Assertions are mostly meant for debugging, but can also be used to specify program constraints (e.g., type tests on entry to functions).

A.3.1.5 Reserved word changes

The word "assert" was added to the list of Python reserved words; "access" was removed (it has now been deprecated in earnest).

A.3.1.6 New dictionary methods

A few convenience methods were added to the built-in dictionary object to avoid the need for manual loops: D.clear( ), D.copy( ), D.update( ), and D.get( ). The first two methods empty and copy dictionaries, respectively. D1.update(D2) is equivalent to the loop:

for k in D2.keys( ): D1[k] = D2[k]

D.get(k) returns D[k] if it exists, or None (or its optional second argument) if the key does not exist.

A.3.1.7 New list methods

List objects have a new method, pop, to fetch and delete the last item of the list:

x = s.pop( ) ...is the same as the two statements... x = s[-1]; del s[-1]

and extend, to concatenate a list of items on the end, in place:

s.extend(x) ...is the same as... s[len(s):len(s)] = x

The pop method can also be passed an index to delete (it defaults to -1). Unlike append, extend is passed an entire list and adds each of its items at the end.

A.3.1.8 "Raw" string constants

In support of regular expressions and Windows, Python allows string constants to be written in the form r"......", which works like a normal string except that Python leaves any backslashes in the string alone. They remain as literal characters rather than being interpreted as special escape codes by Python.

A.3.1.9 Complex number type

Python now supports complex number constants (e.g., 1+3j) and complex arithmetic operations (normal math operators, plus a cmath module with many of the math modules functions for complex numbers).

A.3.1.10 Printing cyclic objects doesn core dump

Objects created with code like L.append(L) are now detected and printed specially by the interpreter. In the past, trying to print cyclic objects caused the interpreter to loop recursively (which eventually led to a core dump).

A.3.1.11 raise without arguments: re-raise

A raise statement without any exception or extra-data arguments now makes Python re-raise the most recently raised uncaught exception.

A.3.1.12 raise forms for class exceptions

Because exceptions can now either be string objects or classes and class instances, you can use any of the following raise statement forms:

raise string # matches except with same string object raise string, data # same, with optional data raise class, instance # matches except with class or its superclass raise instance # same as: raise instance.__class__, instance raise # reraise last exception

You can also use the following three forms, which are for backwards-compatibility with earlier releases where all built-in exceptions were strings:

raise class # same as: raise class( ) (and: raise class, instance) raise class, arg # same as: raise class(arg) raise class, (arg,...) # same as: raise class(args...)

A.3.1.13 Power operator X ** Y

The new ** binary operator computes the left operand raised to the power of the right operand. It works much like the built-in pow function.

A.3.1.14 Generalized sequence assignments

In an assignment (= statements and other assignment contexts), you can now assign any sort of sequence on the right to a list or tuple on the left (e.g., (A,B) = seq, [A,B] = seq ). In the past, the sequence types had to match.

A.3.1.15 Its faster

Python 1.5 has been clocked at almost twice the speed of its predecessors on the Lib/test/pystone.py benchmark. (Ive seen almost a threefold speedup in other tests.)

A.3.2 Library Changes

The following sections describe changes made to the Python standard library.

A.3.2.1 dir(X) now works on more objects

The built-in dir function now reports attributes for modules, classes, and class instances, as well as for built-in objects such as lists, dictionaries, and files. You don need to use members like __methods__ (but you still can).

A.3.2.2 New conversions: int(X), float(X), list(S)

The int and float built-in functions now accept string arguments, and convert from strings to numbers exactly like string.atoi/atof. The new list(S) built-in function converts any sequence to a list, much like the older and obscure map(None, S) trick.

A.3.2.3 The new re regular expression module

A new regular expression module, re, offers full-blown Perl-style regular expression matching. See Chapter 18, for details. The older regex module described in the first edition is still available, but considered obsolete.

A.3.2.4 splitfields/joinfields became split/join

The split and join functions in the string module were generalized to do the same work as the original splitfields and joinfields.

A.3.2.5 Persistence: unpickler no longer calls __init__

Beginning in Python 1.5, the pickle modules unpickler (loader) no longer calls class __init__ methods to recreate pickled class instance objects. This means that classes no longer need defaults for all constructor arguments to be used for persistent objects. To force Python to call the __init_ _ method (as it did before), classes must provide a special __getinitargs__ method; see the library manual for details.

A.3.2.6 Object pickler coded in C: cPickle

An implementation of the pickle module in C is now a standard part of Python. Its called cPickle and is reportedly many times faster than the original pickle. If present, the shelve module loads it instead of pickle automatically.

A.3.2.7 anydbm.open now expects a "c" second argument for prior behavior

To open a DBM file in "create new or open existing for read+write" mode, pass a "c" in argument 2 to anydbm.open. This changed as of Python 1.5.2; passing a "c" now does what passing no second argument used to do (the second argument now defaults to "r" -- read-only). This does not impact shelve.open.

A.3.2.8 rand module replaced by random module

The rand module is now deprecated; use random instead.

A.3.2.9 Assorted Tkinter changes

Tkinter became portable to and sprouted native look-and-feel for all major platforms (Windows, X, Macs). There has been a variety of changes in the Tkinter GUI interface:

StringVar objects can be called

The __call_ _ method for StringVar class objects was dropped in Python 1.4; that means you need to explicitly call their get( )/set( ) methods, instead of calling them with or without arguments.

ScrolledText changed

The ScrolledText widget went through a minor interface change in Python 1.4, which was apparently backed out in release 1.5 due to code breakage (so never mind).

Gridded geometry manager

Tkinter now supports Tks new grid geometry manager. To use it, call the grid method of widget objects (much like pack , but passes row and column numbers, not constraints).

New Tkinter documentation site

Fredrik Lundh now maintains a nice set of Tkinter documentation at http://www.pythonware.com, which provides references and tutorials.

A.3.2.10 CGI module interface change

The CGI interface changed. An older FormContent interface was deprecated in favor of the FieldStorage objects interface. See the library manual for details.

A.3.2.11 site.py, user.py, and PYTHONHOME

These scripts are automatically run by Python on startup, used to tailor initial paths configuration. See the library manuals for details.

A.3.2.12 Assignment to os.environ[key] calls putenv

Assigning to a key in the os.environ dictionary now updates the corresponding environment variable in the C environment. It triggers a call to the C librarys putenv routine such that the changes are reflected in integrated C code layers as well as in the environment of any child processes spawned by the Python program. putenv is now exposed in the os module too (os.putenv).

A.3.2.13 New sys.exc_info( ) tuple

The new exc_info( ) function in the sys module returns a tuple of values corresponding to sys.exc_type and sys.exc_value. These older names access a single global exception; exc_info is specific to the calling thread.

A.3.2.14 The new operator module

There is a new standard module called operator, which provides functions that implement most of the built-in Python expression operators. For instance, operator.add(X,Y) does the same thing as X+Y, but because operator module exports are functions, they are sometimes handy to use in things like map, so you don have to create a function or use a lambda form.

A.3.3 Tool Changes

The following sections describe major Python tool-related changes.

A.3.3.1 JPython (a.k.a. Jython): a Python-to-Java compiler

The new JPython system is an alternative Python implementation that compiles Python programs to Java Virtual Machine ( JVM) bytecode and provides hooks for integrating Python and Java programs. See Chapter 15.

A.3.3.2 MS-Windows ports: COM, Tkinter

The COM interfaces in the Python Windows ports have evolved substantially since the first editions descriptions (it was "OLE" back then); see Chapter 15. Python also now ships as a self-installer for Windows, with built-in support for the Tkinter interface, DBM-style files, and more; its a simple double-click to install today.

A.3.3.3 SWIG growth, C++ shadow classes

The SWIG system has become a primary extension writers tool, with new "shadow classes" for wrapping C++ classes. See Chapter 19.

A.3.3.4 Zope (formerly Bobo): Python objects for the Web

This system for publishing Python objects on the Web has grown to become a popular tool for CGI programmers and web scripters in general. See the Zope section in Chapter 15.

A.3.3.5 HTMLgen: making HTML from Python classes

This tool for generating correct HTML files (web page layouts) from Python class object trees has grown to maturity. See Chapter 15.

A.3.3.6 PMW: Python mega-widgets for Tkinter

The PMW system provides powerful, higher-level widgets for Tkinter-based GUIs in Python. See Chapter 6.

A.3.3.7 IDLE: an integrated development environment GUI

Python now ships with a point-and-click development interface named IDLE. Written in Python using the Tkinter GUI library, IDLE either comes in the source librarys Tools directory or is automatically installed with Python itself (on Windows, see IDLEs entry in the Python menu within your Start button menus). IDLE offers a syntax-coloring text editor, a graphical debugger, an object browser, and more. If you have Python with Tk support enabled and are accustomed to more advanced development interfaces, IDLE provides a feature-rich alternative to the traditional Python command line. IDLE does not provide a GUI builder today.

A.3.3.8 Other tool growth: PIL, NumPy, Database API

The PIL image processing and NumPy numeric programming systems have matured considerably, and a portable database API for Python has been released. See Chapter 6 and Chapter 16.

A.3.4 Python/C Integration API Changes

The following sections describe changes made to the Python C API.

A.3.4.1 A single Python.h header file

All useful Python symbols are now exported in the single Python.h header file; no other header files need be imported in most cases.

A.3.4.2 A single libpython*.a C library file

All Python interpreter code is now packaged in a single library file when you build Python. For instance, under Python 1.5, you need only link in libpython1.5.a when embedding Python (instead of the older schemes four libraries plus .os).

A.3.4.3 The "Great (Grand?) Renaming" is complete

All exposed Python symbols now start with a "Py" prefix.

A.3.4.4 Threading support, multiple interpreters

A handful of new API tools provide better support for threads when embedding Python. For instance, there are tools for finalizing Python (Py_Finalize) and for creating "multiple interpreters" (Py_NewInterpreter).

Note that spawning Python language threads may be a viable alternative to C-level threads, and multiple namespaces are often sufficient to isolate names used in independent system components; both schemes are easier to manage than multiple interpreters and threads. But in some threaded programs, its also useful to have one copy of system modules and structures per thread, and this is where multiple interpreters come in handy (e.g., without one copy per thread, imports might find an already-loaded module in the sys.modules table if it was imported by a different thread). See the new C API documentation manuals for details.

A.3.4.5 New Python C API documentation

There is a new reference manual that ships with Python and documents major C API tools and behavior. Its not fully fleshed out yet, but its a useful start.

Категории