blob: 26baa57d091a3fc2eb6c91a86d893740d593f458 [file] [log] [blame]
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +00001\documentclass{howto}
2
3% $Id$
4
5\title{What's New in Python 2.1}
Andrew M. Kuchling81b6ae72001-02-11 16:55:39 +00006\release{0.06}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +00007\author{A.M. Kuchling}
8\authoraddress{\email{amk1@bigfoot.com}}
9\begin{document}
10\maketitle\tableofcontents
11
12\section{Introduction}
13
Andrew M. Kuchlingf33c1182001-01-23 02:48:26 +000014{\large This document is a draft, and is subject to change until
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +000015the final version of Python 2.1 is released. Currently it is up to date
Andrew M. Kuchling74d18ed2001-02-28 22:22:40 +000016for Python 2.1 beta 1. Please send any comments, bug reports, or
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +000017questions, no matter how minor, to \email{amk1@bigfoot.com}. }
Andrew M. Kuchlingf33c1182001-01-23 02:48:26 +000018
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +000019It's that time again... time for a new Python release, version 2.1.
20One recent goal of the Python development team has been to accelerate
21the pace of new releases, with a new release coming every 6 to 9
22months. 2.1 is the first release to come out at this faster pace, with
23the first alpha appearing in January, 3 months after the final version
24of 2.0 was released.
25
26This article explains the new features in 2.1. While there aren't as
27many changes in 2.1 as there were in Python 2.0, there are still some
28pleasant surprises in store. 2.1 is the first release to be steered
29through the use of Python Enhancement Proposals, or PEPs, so most of
30the sizable changes have accompanying PEPs that provide more complete
31documentation and a design rationale for the change. This article
32doesn't attempt to document the new features completely, but simply
33provides an overview of the new features for Python programmers.
34Refer to the Python 2.1 documentation, or to the specific PEP, for
35more details about any new feature that particularly interests you.
36
Andrew M. Kuchling8bad9932001-02-28 22:39:15 +000037Currently 2.1 is available in a beta release, and the final release is
38planned for April 2001.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +000039
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +000040%======================================================================
41\section{PEP 227: Nested Scopes}
42
43The largest change in Python 2.1 is to Python's scoping rules. In
44Python 2.0, at any given time there are at most three namespaces used
45to look up variable names: local, module-level, and the built-in
46namespace. This often surprised people because it didn't match their
47intuitive expectations. For example, a nested recursive function
48definition doesn't work:
49
50\begin{verbatim}
51def f():
52 ...
53 def g(value):
54 ...
55 return g(value-1) + 1
56 ...
57\end{verbatim}
58
59The function \function{g()} will always raise a \exception{NameError}
60exception, because the binding of the name \samp{g} isn't in either
61its local namespace or in the module-level namespace. This isn't much
62of a problem in practice (how often do you recursively define interior
63functions like this?), but this also made using the \keyword{lambda}
64statement clumsier, and this was a problem in practice. In code which
65uses \keyword{lambda} you can often find local variables being copied
66by passing them as the default values of arguments.
67
68\begin{verbatim}
69def find(self, name):
70 "Return list of any entries equal to 'name'"
71 L = filter(lambda x, name=name: x == name,
72 self.list_attribute)
73 return L
74\end{verbatim}
75
76The readability of Python code written in a strongly functional style
77suffers greatly as a result.
78
79The most significant change to Python 2.1 is that static scoping has
80been added to the language to fix this problem. As a first effect,
81the \code{name=name} default argument is now unnecessary in the above
82example. Put simply, when a given variable name is not assigned a
83value within a function (by an assignment, or the \keyword{def},
84\keyword{class}, or \keyword{import} statements), references to the
85variable will be looked up in the local namespace of the enclosing
86scope. A more detailed explanation of the rules, and a dissection of
87the implementation, can be found in the PEP.
88
89This change may cause some compatibility problems for code where the
90same variable name is used both at the module level and as a local
91variable within a function that contains further function definitions.
92This seems rather unlikely though, since such code would have been
Andrew M. Kuchling61af5602001-03-03 03:25:04 +000093pretty confusing to read in the first place.
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +000094
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +000095One side effect of the change is that the \code{from \var{module}
96import *} and \keyword{exec} statements have been made illegal inside
97a function scope under certain conditions. The Python reference
98manual has said all along that \code{from \var{module} import *} is
99only legal at the top level of a module, but the CPython interpreter
100has never enforced this before. As part of the implementation of
101nested scopes, the compiler which turns Python source into bytecodes
102has to generate different code to access variables in a containing
103scope. \code{from \var{module} import *} and \keyword{exec} make it
104impossible for the compiler to figure this out, because they add names
105to the local namespace that are unknowable at compile time.
106Therefore, if a function contains function definitions or
107\keyword{lambda} expressions with free variables, the compiler will
108flag this by raising a \exception{SyntaxError} exception.
109
110To make the preceding explanation a bit clearer, here's an example:
111
112\begin{verbatim}
113x = 1
114def f():
115 # The next line is a syntax error
116 exec 'x=2'
117 def g():
118 return x
119\end{verbatim}
120
121Line 4 containing the \keyword{exec} statement is a syntax error,
122since \keyword{exec} would define a new local variable named \samp{x}
123whose value should be accessed by \function{g()}.
124
125This shouldn't be much of a limitation, since \keyword{exec} is rarely
126used in most Python code (and when it is used, it's often a sign of a
127poor design anyway).
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000128
Andrew M. Kuchling61af5602001-03-03 03:25:04 +0000129Compatibility concerns have led to nested scopes being introduced
130gradually; in Python 2.1, they aren't enabled by default, but can be
131turned on within a module by using a future statement as described in
132PEP 236. (See the following section for further discussion of PEP
133236.) In Python 2.2, nested scopes will become the default and there
134will be no way to turn them off, but users will have had all of 2.1's
135lifetime to fix any breakage resulting from their introduction.
136
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000137\begin{seealso}
138
139\seepep{227}{Statically Nested Scopes}{Written and implemented by
140Jeremy Hylton.}
141
142\end{seealso}
143
144
145%======================================================================
Andrew M. Kuchling74d18ed2001-02-28 22:22:40 +0000146\section{PEP 236: \module{__future__} Directives}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000147
Andrew M. Kuchling61af5602001-03-03 03:25:04 +0000148The reaction to nested scopes was widespread concern about the dangers
149of breaking code with the 2.1 release, and it was strong enough to
150make the Pythoneers take a more conservative approach. This approach
151consists of introducing a convention for enabling optional
152functionality in release N that will become compulsory in release N+1.
153
154The syntax uses a \code{from...import} statement using the reserved
155module name \module{__future__}. Nested scopes can be enabled by the
156following statement:
157
158\begin{verbatim}
159from __future__ import nested_scopes
160\end{verbatim}
161
162While it looks like a normal \keyword{import} statement, it's not;
163there are strict rules on where such a future statement can be put.
164They can only be at the top of a module, and must precede any Python
165code or regular \keyword{import} statements. This is because such
166statements can affect how the Python bytecode compiler parses code and
167generates bytecode, so they must precede any statement that will
168result in bytecodes being produced.
169
170\begin{seealso}
171
172\seepep{236}{Back to the \module{__future__}}{Written by Tim Peters,
173and primarily implemented by Jeremy Hylton.}
174
175\end{seealso}
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000176
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000177%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000178\section{PEP 207: Rich Comparisons}
179
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000180In earlier versions, Python's support for implementing comparisons on
181user-defined classes and extension types was quite simple. Classes
182could implement a \method{__cmp__} method that was given two instances
183of a class, and could only return 0 if they were equal or +1 or -1 if
184they weren't; the method couldn't raise an exception or return
185anything other than a Boolean value. Users of Numeric Python often
186found this model too weak and restrictive, because in the
187number-crunching programs that numeric Python is used for, it would be
188more useful to be able to perform elementwise comparisons of two
189matrices, returning a matrix containing the results of a given
190comparison for each element. If the two matrices are of different
191sizes, then the compare has to be able to raise an exception to signal
192the error.
193
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000194In Python 2.1, rich comparisons were added in order to support this
195need. Python classes can now individually overload each of the
196\code{<}, \code{<=}, \code{>}, \code{>=}, \code{==}, and \code{!=}
197operations. The new magic method names are:
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000198
199\begin{tableii}{c|l}{code}{Operation}{Method name}
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000200 \lineii{<}{\method{__lt__}} \lineii{<=}{\method{__le__}}
201 \lineii{>}{\method{__gt__}} \lineii{>=}{\method{__ge__}}
202 \lineii{==}{\method{__eq__}} \lineii{!=}{\method{__ne__}}
203 \end{tableii}
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000204
205(The magic methods are named after the corresponding Fortran operators
206\code{.LT.}. \code{.LE.}, \&c. Numeric programmers are almost
207certainly quite familar with these names and will find them easy to
208remember.)
209
210Each of these magic methods is of the form \code{\var{method}(self,
211other)}, where \code{self} will be the object on the left-hand side of
212the operator, while \code{other} will be the object on the right-hand
213side. For example, the expression \code{A < B} will cause
214\code{A.__lt__(B)} to be called.
215
216Each of these magic methods can return anything at all: a Boolean, a
217matrix, a list, or any other Python object. Alternatively they can
218raise an exception if the comparison is impossible, inconsistent, or
219otherwise meaningless.
220
221The built-in \function{cmp(A,B)} function can use the rich comparison
222machinery, and now accepts an optional argument specifying which
223comparison operation to use; this is given as one of the strings
224\code{"<"}, \code{"<="}, \code{">"}, \code{">="}, \code{"=="}, or
225\code{"!="}. If called without the optional third argument,
226\function{cmp()} will only return -1, 0, or +1 as in previous versions
227of Python; otherwise it will call the appropriate method and can
228return any Python object.
229
230There are also corresponding changes of interest to C programmers;
231there's a new slot \code{tp_richcmp} in type objects and an API for
232performing a given rich comparison. I won't cover the C API here, but
Andrew M. Kuchlingbf140142001-02-28 22:10:07 +0000233will refer you to PEP 207, or to 2.1's C API documentation, for the
234full list of related functions.
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000235
236\begin{seealso}
237
238\seepep{207}{Rich Comparisions}{Written by Guido van Rossum, heavily
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000239based on earlier work by David Ascher, and implemented by Guido van
240Rossum.}
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000241
242\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000243
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000244%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000245\section{PEP 230: Warning Framework}
246
247Over its 10 years of existence, Python has accumulated a certain
248number of obsolete modules and features along the way. It's difficult
249to know when a feature is safe to remove, since there's no way of
Andrew M. Kuchlingf33c1182001-01-23 02:48:26 +0000250knowing how much code uses it --- perhaps no programs depend on the
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000251feature, or perhaps many do. To enable removing old features in a
252more structured way, a warning framework was added. When the Python
253developers want to get rid of a feature, it will first trigger a
254warning in the next version of Python. The following Python version
255can then drop the feature, and users will have had a full release
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000256cycle to remove uses of the old feature.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000257
258Python 2.1 adds the warning framework to be used in this scheme. It
259adds a \module{warnings} module that provide functions to issue
260warnings, and to filter out warnings that you don't want to be
261displayed. Third-party modules can also use this framework to
262deprecate old features that they no longer wish to support.
263
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000264For example, in Python 2.1 the \module{regex} module is deprecated, so
265importing it causes a warning to be printed:
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000266
267\begin{verbatim}
268>>> import regex
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000269__main__:1: DeprecationWarning: the regex module
270 is deprecated; please use the re module
271>>>
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000272\end{verbatim}
273
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000274Warnings can be issued by calling the \function{warnings.warn}
275function:
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000276
277\begin{verbatim}
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000278warnings.warn("feature X no longer supported")
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000279\end{verbatim}
280
281The first parameter is the warning message; an additional optional
282parameters can be used to specify a particular warning category.
283
284Filters can be added to disable certain warnings; a regular expression
285pattern can be applied to the message or to the module name in order
286to suppress a warning. For example, you may have a program that uses
287the \module{regex} module and not want to spare the time to convert it
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000288to use the \module{re} module right now. The warning can be
289suppressed by calling
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000290
291\begin{verbatim}
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000292import warnings
293warnings.filterwarnings(action = 'ignore',
294 message='.*regex module is deprecated',
295 category=DeprecationWarning,
296 module = '__main__')
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000297\end{verbatim}
298
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000299This adds a filter that will apply only to warnings of the class
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000300\class{DeprecationWarning} triggered in the \module{__main__} module,
301and applies a regular expression to only match the message about the
302\module{regex} module being deprecated, and will cause such warnings
303to be ignored. Warnings can also be printed only once, printed every
304time the offending code is executed, or turned into exceptions that
305will cause the program to stop (unless the exceptions are caught in
306the usual way, of course).
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000307
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000308Functions were also added to Python's C API for issuing warnings;
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000309refer to PEP 230 or to Python's API documentation for the details.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000310
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000311\begin{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000312
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000313\seepep{5}{Guidelines for Language Evolution}{Written
314by Paul Prescod, to specify procedures to be followed when removing
315old features from Python. The policy described in this PEP hasn't
316been officially adopted, but the eventual policy probably won't be too
317different from Prescod's proposal.}
318
319\seepep{230}{Warning Framework}{Written and implemented by Guido van
320Rossum.}
321
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000322\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000323
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000324%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000325\section{PEP 229: New Build System}
326
327When compiling Python, the user had to go in and edit the
328\file{Modules/Setup} file in order to enable various additional
329modules; the default set is relatively small and limited to modules
330that compile on most Unix platforms. This means that on Unix
331platforms with many more features, most notably Linux, Python
332installations often don't contain all useful modules they could.
333
334Python 2.0 added the Distutils, a set of modules for distributing and
335installing extensions. In Python 2.1, the Distutils are used to
336compile much of the standard library of extension modules,
Andrew M. Kuchlingf33c1182001-01-23 02:48:26 +0000337autodetecting which ones are supported on the current machine. It's
338hoped that this will make Python installations easier and more
339featureful.
340
341Instead of having to edit the \file{Modules/Setup} file in order to
342enable modules, a \file{setup.py} script in the top directory of the
343Python source distribution is run at build time, and attempts to
344discover which modules can be enabled by examining the modules and
Andrew M. Kuchling8bad9932001-02-28 22:39:15 +0000345header files on the system. If a module is configured in
346\file{Modules/Setup}, the \file{setup.py} script won't attempt to
347compile that module and will defer to the \file{Modules/Setup} file's
348contents. This provides a way to specific any strange command-line
349flags or libraries that are required for a specific platform.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000350
Andrew M. Kuchling4308d3c2001-01-29 17:36:53 +0000351In another far-reaching change to the build mechanism, Neil
352Schemenauer restructured things so Python now uses a single makefile
353that isn't recursive, instead of makefiles in the top directory and in
Andrew M. Kuchling8bad9932001-02-28 22:39:15 +0000354each of the \file{Python/}, \file{Parser/}, \file{Objects/}, and
355\file{Modules/} subdirectories. This makes building Python faster
356and also makes hacking the Makefiles clearer and simpler.
Andrew M. Kuchling4308d3c2001-01-29 17:36:53 +0000357
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000358\begin{seealso}
359
360\seepep{229}{Using Distutils to Build Python}{Written
361and implemented by A.M. Kuchling.}
362
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000363\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000364
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000365%======================================================================
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000366\section{PEP 205: Weak References}
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000367
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000368Weak references, available through the \module{weakref} module, are a
369minor but useful new data type in the Python programmer's toolbox.
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000370
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000371Storing a reference to an object (say, in a dictionary or a list) has
372the side effect of keeping that object alive forever. There are a few
373specific cases where this behaviour is undesirable, object caches
374being the most common one, and another being circular references in
375data structures such as trees.
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000376
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000377For example, consider a memoizing function that caches the results of
378another function \function{f(\var{x})} by storing the function's
379argument and its result in a dictionary:
380
381\begin{verbatim}
382_cache = {}
383def memoize(x):
384 if _cache.has_key(x):
385 return _cache[x]
386
387 retval = f(x)
388
389 # Cache the returned object
390 _cache[x] = retval
391
392 return retval
393\end{verbatim}
394
395This version works for simple things such as integers, but it has a
396side effect; the \code{_cache} dictionary holds a reference to the
397return values, so they'll never be deallocated until the Python
398process exits and cleans up This isn't very noticeable for integers,
399but if \function{f()} returns an object, or a data structure that
400takes up a lot of memory, this can be a problem.
401
402Weak references provide a way to implement a cache that won't keep
403objects alive beyond their time. If an object is only accessible
404through weak references, the object will be deallocated and the weak
405references will now indicate that the object it referred to no longer
406exists. A weak reference to an object \var{obj} is created by calling
407\code{wr = weakref.ref(\var{obj})}. The object being referred to is
408returned by calling the weak reference as if it were a function:
409\code{wr()}. It will return the referenced object, or \code{None} if
410the object no longer exists.
411
412This makes it possible to write a \function{memoize()} function whose
413cache doesn't keep objects alive, by storing weak references in the
414cache.
415
416\begin{verbatim}
417_cache = {}
418def memoize(x):
419 if _cache.has_key(x):
420 obj = _cache[x]()
421 # If weak reference object still exists,
422 # return it
423 if obj is not None: return obj
424
425 retval = f(x)
426
427 # Cache a weak reference
428 _cache[x] = weakref.ref(retval)
429
430 return retval
431\end{verbatim}
432
433The \module{weakref} module also allows creating proxy objects which
434behave like weak references --- an object referenced only by proxy
435objects is deallocated -- but instead of requiring an explicit call to
436retrieve the object, the proxy transparently forwards all operations
437to the object as long as the object still exists. If the object is
438deallocated, attempting to use a proxy will cause a
439\exception{weakref.ReferenceError} exception to be raised.
440
441\begin{verbatim}
442proxy = weakref.proxy(obj)
443proxy.attr # Equivalent to obj.attr
444proxy.meth() # Equivalent to obj.meth()
445del obj
446proxy.attr # raises weakref.ReferenceError
447\end{verbatim}
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000448
449\begin{seealso}
450
451\seepep{205}{Weak References}{Written and implemented by
452Fred~L. Drake,~Jr.}
453
454\end{seealso}
455
456%======================================================================
Andrew M. Kuchling74d18ed2001-02-28 22:22:40 +0000457\section{PEP 235: Case-Insensitive Platforms and \keyword{import}}
458
Andrew M. Kuchling8bad9932001-02-28 22:39:15 +0000459Some operating systems have filesystems that are case-insensitive,
460MacOS and Windows being the primary examples; on these systems, it's
461impossible to distinguish the filenames \samp{FILE.PY} and
462\samp{file.py}, even though they do store the file's name
463in its original case (they're case-preserving, too).
464
465In Python 2.1, the \keyword{import} statement will work to simulate
466case-sensitivity on case-insensitive platforms. Python will now
467search for the first case-sensitive match by default, raising an
468\exception{ImportError} if no such file is found, so \code{import file}
469will not import a module named \samp{FILE.PY}. Case-insensitive
470matching can be requested by setting the PYTHONCASEOK environment
471variable before starting the Python interpreter.
Andrew M. Kuchling74d18ed2001-02-28 22:22:40 +0000472
473%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000474\section{PEP 217: Interactive Display Hook}
475
476When using the Python interpreter interactively, the output of
477commands is displayed using the built-in \function{repr()} function.
478In Python 2.1, the variable \module{sys.displayhook} can be set to a
479callable object which will be called instead of \function{repr()}.
480For example, you can set it to a special pretty-printing function:
481
482\begin{verbatim}
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000483>>> # Create a recursive data structure
484... L = [1,2,3]
485>>> L.append(L)
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000486>>> L # Show Python's default output
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000487[1, 2, 3, [...]]
488>>> # Use pprint.pprint() as the display function
489... import sys, pprint
490>>> sys.displayhook = pprint.pprint
491>>> L
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000492[1, 2, 3, <Recursion on list with id=135143996>]
493>>>
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000494\end{verbatim}
495
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000496\begin{seealso}
497
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000498\seepep{217}{Display Hook for Interactive Use}{Written and implemented
499by Moshe Zadka.}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000500
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000501\end{seealso}
502
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000503%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000504\section{PEP 208: New Coercion Model}
505
506How numeric coercion is done at the C level was significantly
507modified. This will only affect the authors of C extensions to
508Python, allowing them more flexibility in writing extension types that
509support numeric operations.
510
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000511Extension types can now set the type flag \code{Py_TPFLAGS_CHECKTYPES}
512in their \code{PyTypeObject} structure to indicate that they support
513the new coercion model. In such extension types, the numeric slot
514functions can no longer assume that they'll be passed two arguments of
515the same type; instead they may be passed two arguments of differing
516types, and can then perform their own internal coercion. If the slot
517function is passed a type it can't handle, it can indicate the failure
518by returning a reference to the \code{Py_NotImplemented} singleton
519value. The numeric functions of the other type will then be tried,
520and perhaps they can handle the operation; if the other type also
521returns \code{Py_NotImplemented}, then a \exception{TypeError} will be
522raised. Numeric methods written in Python can also return
523\code{Py_NotImplemented}, causing the interpreter to act as if the
524method did not exist (perhaps raising a \exception{TypeError}, perhaps
525trying another object's numeric methods).
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000526
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000527\begin{seealso}
528
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000529\seepep{208}{Reworking the Coercion Model}{Written and implemented by
530Neil Schemenauer, heavily based upon earlier work by Marc-Andr\'e
531Lemburg. Read this to understand the fine points of how numeric
532operations will now be processed at the C level.}
533
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000534\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000535
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000536%======================================================================
Andrew M. Kuchling81b6ae72001-02-11 16:55:39 +0000537\section{New and Improved Modules}
538
539\begin{itemize}
540
Andrew M. Kuchling74d18ed2001-02-28 22:22:40 +0000541\item Ka-Ping Yee contributed two new modules: \module{inspect.py}, a module for
542getting information about live Python code, and \module{pydoc.py}, a
543module for interactively converting docstrings to HTML or text.
544As a bonus, \file{Tools/scripts/pydoc}, which is now automatically
545installed, uses \module{pydoc.py} to display documentation given a Python module, package, or class name. For example,
546\samp{pydoc xml.dom} displays the following:
547
548\begin{verbatim}
549Python Library Documentation: package xml.dom in xml
550
551NAME
552 xml.dom - W3C Document Object Model implementation for Python.
553
554FILE
555 /usr/local/lib/python2.1/xml/dom/__init__.pyc
556
557DESCRIPTION
558 The Python mapping of the Document Object Model is documented in the
559 Python Library Reference in the section on the xml.dom package.
560
561 This package contains the following modules:
562 ...
563\end{verbatim}
564
565\file{pydoc} quickly becomes addictive; try it out!
566
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000567\item The \module{doctest} module provides a testing framework based
568on running embedded examples in docstrings and comparing the results
569against the expected output. Contributed by Tim Peters.
570
571\item The \module{difflib} module contains a class,
572\class{SequenceMatcher}, which compares two sequences and computes the
573changes required to transform one sequence into the other. For
574example, this module can be used to write a tool similar to the Unix
575\program{diff} program, and in fact the sample program
576\file{Tools/scripts/ndiff.py} demonstrates how to write such a script.
577
Andrew M. Kuchling81b6ae72001-02-11 16:55:39 +0000578\item \module{curses.panel}, a wrapper for the panel library, part of
579ncurses and of SYSV curses, was contributed by Thomas Gellekum. The
580panel library provides windows with the additional feature of depth.
581Windows can be moved higher or lower in the depth ordering, and the
582panel library figures out where panels overlap and which sections are
583visible.
584
585\item The PyXML package has gone through a few releases since Python
5862.0, and Python 2.1 includes an updated version of the \module{xml}
Andrew M. Kuchling74d18ed2001-02-28 22:22:40 +0000587package. Some of the noteworthy changes include support for Expat 1.2
588and later versions, the ability for Expat parsers to handle files in
589any encoding supported by Python, and various bugfixes for SAX, DOM,
590and the \module{minidom} module.
Andrew M. Kuchling81b6ae72001-02-11 16:55:39 +0000591
592\item Various functions in the \module{time} module, such as
593\function{asctime()} and \function{localtime()}, require a floating
594point argument containing the time in seconds since the epoch. The
595most common use of these functions is to work with the current time,
596so the floating point argument has been made optional; when a value
597isn't provided, the current time will be used. For example, log file
598entries usually need a string containing the current time; in Python
5992.1, \code{time.asctime()} can be used, instead of the lengthier
600\code{time.asctime(time.localtime(time.time()))} that was previously
601required.
602
603This change was proposed and implemented by Thomas Wouters.
604
605\item The \module{ftplib} module now defaults to retrieving files in
606passive mode, because passive mode is more likely to work from behind
607a firewall. This request came from the Debian bug tracking system,
608since other Debian packages use \module{ftplib} to retrieve files and
609then don't work from behind a firewall. It's deemed unlikely that
610this will cause problems for anyone, because Netscape defaults to
611passive mode and few people complain, but if passive mode is
612unsuitable for your application or network setup, call
613\method{set_pasv(0)} on FTP objects to disable passive mode.
614
615\item Support for raw socket access has been added to the
616\module{socket} module, contributed by Grant Edwards.
617
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000618\item A new implementation-dependent function, \function{sys._getframe(\optional{depth})},
619has been added to return a given frame object from the current call stack.
620\function{sys._getframe()} returns the frame at the top of the call stack;
621if the optional integer argument \var{depth} is supplied, the function returns the frame
622that is \var{depth} calls below the top of the stack. For example, \code{sys._getframe(1)}
623returns the caller's frame object.
624
625This function is only present in CPython, not in Jython or the .NET
626implementation. Use it for debugging, and resist the temptation to
627put it into production code.
628
629
Andrew M. Kuchling81b6ae72001-02-11 16:55:39 +0000630
631\end{itemize}
632
633%======================================================================
Andrew M. Kuchling74d18ed2001-02-28 22:22:40 +0000634\section{Other Changes and Fixes}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000635
636There were relatively few smaller changes made in Python 2.1 due to
637the shorter release cycle. A search through the CVS change logs turns
Andrew M. Kuchling81df7be2001-03-02 21:19:38 +0000638up 117 patches applied, and 136 bugs fixed; both figures are likely to
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000639be underestimates. Some of the more notable changes are:
640
641\begin{itemize}
642
Andrew M. Kuchlingbf140142001-02-28 22:10:07 +0000643
644\item A specialized object allocator is now optionally available, that
645should be faster than the system \function{malloc()} and have less
646memory overhead. The allocator uses C's \function{malloc()} function
647to get large pools of memory, and then fulfills smaller memory
648requests from these pools. It can be enabled by providing the
Andrew M. Kuchling45bbda22001-03-10 16:49:07 +0000649\longprogramopt{with-pymalloc} option to the \program{configure} script; see
Andrew M. Kuchling74d18ed2001-02-28 22:22:40 +0000650\file{Objects/obmalloc.c} for the implementation details.
Andrew M. Kuchlingbf140142001-02-28 22:10:07 +0000651Contributed by Vladimir Marangozov.
652
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000653\item The speed of line-oriented file I/O has been improved because
654people often complain about its lack of speed, and because it's often
655been used as a na\"ive benchmark. The \method{readline()} method of
656file objects has therefore been rewritten to be much faster. The
657exact amount of the speedup will vary from platform to platform
658depending on how slow the C library's \function{getc()} was, but is
659around 66\%, and potentially much faster on some particular operating
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000660systems. Tim Peters did much of the benchmarking and coding for this
661change, motivated by a discussion in comp.lang.python.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000662
663A new module and method for file objects was also added, contributed
664by Jeff Epler. The new method, \method{xreadlines()}, is similar to
665the existing \function{xrange()} built-in. \function{xreadlines()}
666returns an opaque sequence object that only supports being iterated
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000667over, reading a line on every iteration but not reading the entire
Andrew M. Kuchlingf33c1182001-01-23 02:48:26 +0000668file into memory as the existing \method{readlines()} method does.
669You'd use it like this:
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000670
671\begin{verbatim}
672for line in sys.stdin.xreadlines():
673 # ... do something for each line ...
674 ...
675\end{verbatim}
676
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000677For a fuller discussion of the line I/O changes, see the python-dev
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000678summary for January 1-15, 2001.
Andrew M. Kuchling91834c62001-01-22 19:51:13 +0000679
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000680\item A new method, \method{popitem()}, was added to dictionaries to
681enable destructively iterating through the contents of a dictionary;
682this can be faster for large dictionaries because .
683\code{D.popitem()} removes a random \code{(\var{key}, \var{value})}
684pair from the dictionary and returns it as a 2-tuple. This was
685implemented mostly by Tim Peters and Guido van Rossum, after a
686suggestion and preliminary patch by Moshe Zadka.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000687
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000688\item Modules can now control which names are imported when \code{from
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000689\var{module} import *} is used, by defining an \code{__all__}
690attribute containing a list of names that will be imported. One
691common complaint is that if the module imports other modules such as
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000692\module{sys} or \module{string}, \code{from \var{module} import *}
693will add them to the importing module's namespace. To fix this,
694simply list the public names in \code{__all__}:
695
696\begin{verbatim}
697# List public names
698__all__ = ['Database', 'open']
699\end{verbatim}
700
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000701A stricter version of this patch was first suggested and implemented
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000702by Ben Wolfson, but after some python-dev discussion, a weaker final
703version was checked in.
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000704
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000705\item Applying \function{repr()} to strings previously used octal
706escapes for non-printable characters; for example, a newline was
707\code{'\e 012'}. This was a vestigial trace of Python's C ancestry, but
708today octal is of very little practical use. Ka-Ping Yee suggested
709using hex escapes instead of octal ones, and using the \code{\e n},
710\code{\e t}, \code{\e r} escapes for the appropriate characters, and
711implemented this new formatting.
Andrew M. Kuchling4308d3c2001-01-29 17:36:53 +0000712
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000713\item Syntax errors detected at compile-time can now raise exceptions
714containing the filename and line number of the error, a pleasant side
715effect of the compiler reorganization done by Jeremy Hylton.
716
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000717\item C extensions which import other modules have been changed to use
718\function{PyImport_ImportModule()}, which means that they will use any
719import hooks that have been installed. This is also encouraged for
720third-party extensions that need to import some other module from C
721code.
722
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000723\item The size of the Unicode character database was shrunk by another
724340K thanks to Fredrik Lundh.
Andrew M. Kuchling91834c62001-01-22 19:51:13 +0000725
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000726\end{itemize}
727
Andrew M. Kuchling74d18ed2001-02-28 22:22:40 +0000728And there's the usual list of minor bugfixes, minor memory leaks,
729docstring edits, and other tweaks, too lengthy to be worth itemizing;
730see the CVS logs for the full details if you want them.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000731
732
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000733%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000734\section{Acknowledgements}
735
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000736The author would like to thank the following people for offering
737suggestions on various drafts of this article: Graeme Cross, David
738Goodger, Jay Graves, Michael Hudson, Marc-Andr\'e Lemburg, Fredrik
739Lundh, Neil Schemenauer, Thomas Wouters.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000740
741\end{document}