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.
The following sections describe changes made to the Python language itself.
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.
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.
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.
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).
The word "assert" was added to the list of Python reserved words; "access" was removed (it has now been deprecated in earnest).
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.
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.
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.
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).
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 raise statement without any exception or extra-data arguments now makes Python re-raise the most recently raised uncaught exception.
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...)
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.
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.
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.)
The following sections describe changes made to the Python standard library.
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).
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 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.
The split and join functions in the string module were generalized to do the same work as the original splitfields and joinfields.
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.
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.
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.
The rand module is now deprecated; use random instead.
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:
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.
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).
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).
Fredrik Lundh now maintains a nice set of Tkinter documentation at http://www.pythonware.com, which provides references and tutorials.
The CGI interface changed. An older FormContent interface was deprecated in favor of the FieldStorage objects interface. See the library manual for details.
These scripts are automatically run by Python on startup, used to tailor initial paths configuration. See the library manuals for details.
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).
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.
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.
The following sections describe major Python tool-related changes.
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.
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.
The SWIG system has become a primary extension writers tool, with new "shadow classes" for wrapping C++ classes. See Chapter 19.
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.
This tool for generating correct HTML files (web page layouts) from Python class object trees has grown to maturity. See Chapter 15.
The PMW system provides powerful, higher-level widgets for Tkinter-based GUIs in Python. See Chapter 6.
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.
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.
The following sections describe changes made to the Python C API.
All useful Python symbols are now exported in the single Python.h header file; no other header files need be imported in most cases.
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).
All exposed Python symbols now start with a "Py" prefix.
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.
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.
Introducing Python
Part I: System Interfaces
System Tools
Parallel System Tools
Larger System Examples I
Larger System Examples II
Part II: GUI Programming
Graphical User Interfaces
A Tkinter Tour, Part 1
A Tkinter Tour, Part 2
Larger GUI Examples
Part III: Internet Scripting
Network Scripting
Client-Side Scripting
Server-Side Scripting
Larger Web Site Examples I
Larger Web Site Examples II
Advanced Internet Topics
Part IV: Assorted Topics
Databases and Persistence
Data Structures
Text and Language
Part V: Integration
Extending Python
Embedding Python
VI: The End
Conclusion Python and the Development Cycle