blob: 25ea1f0681bc9a8db2fb90a11ec75a27be02490d [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. Kuchling91834c62001-01-22 19:51:13 +00006\release{0.03}
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
14It's that time again... time for a new Python release, version 2.1.
15One recent goal of the Python development team has been to accelerate
16the pace of new releases, with a new release coming every 6 to 9
17months. 2.1 is the first release to come out at this faster pace, with
18the first alpha appearing in January, 3 months after the final version
19of 2.0 was released.
20
21This article explains the new features in 2.1. While there aren't as
22many changes in 2.1 as there were in Python 2.0, there are still some
23pleasant surprises in store. 2.1 is the first release to be steered
24through the use of Python Enhancement Proposals, or PEPs, so most of
25the sizable changes have accompanying PEPs that provide more complete
26documentation and a design rationale for the change. This article
27doesn't attempt to document the new features completely, but simply
28provides an overview of the new features for Python programmers.
29Refer to the Python 2.1 documentation, or to the specific PEP, for
30more details about any new feature that particularly interests you.
31
32Currently 2.1 is available in an alpha release, but the release
33schedule calls for a beta release by late February 2001, and a final
34release in April 2001.
35
36% ======================================================================
37\section{PEP 232: Function Attributes}
38
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +000039In Python 2.1, functions can now have arbitrary
40information attached to them. People were often using docstrings to hold
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +000041information about functions and methods, because the \code{__doc__}
42attribute was the only way of attaching any information to a function.
43For example, in the Zope Web application server, functions are marked
44as safe for public access by having a docstring, and in John Aycock's
45SPARK parsing framework, docstrings hold parts of the BNF grammar to
46be parsed. This overloading is unfortunate, since docstrings are
47really intended to hold a function's documentation, and means you
48can't properly document functions intended for private use in Zope.
49
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +000050Attributes can now be set and retrieved on functions, using the
51regular Python syntax:
52
53\begin{verbatim}
54def f(): pass
55
56f.publish = 1
57f.secure = 1
58f.grammar = "A ::= B (C D)*"
59\end{verbatim}
60
61The dictionary containing attributes can be accessed as
62\member{__dict__}. Unlike the \member{__dict__} attribute of
63class instances, in functions you can actually assign a new dictionary
64to \member{__dict__}, though the new value is restricted to a
65regular Python dictionary; you can't be tricky and set it to a
66\class{UserDict} instance, a DBM file, or any other random mapping
67object.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +000068
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +000069\begin{seealso}
70
71\seepep{232}{Function Attributes}{Written and implemented by Barry Warsaw.}
72
73\end{seealso}
74
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +000075% ======================================================================
76\section{PEP 207: Rich Comparisons}
77
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +000078In earlier versions, Python's support for implementing comparisons on
79user-defined classes and extension types was quite simple. Classes
80could implement a \method{__cmp__} method that was given two instances
81of a class, and could only return 0 if they were equal or +1 or -1 if
82they weren't; the method couldn't raise an exception or return
83anything other than a Boolean value. Users of Numeric Python often
84found this model too weak and restrictive, because in the
85number-crunching programs that numeric Python is used for, it would be
86more useful to be able to perform elementwise comparisons of two
87matrices, returning a matrix containing the results of a given
88comparison for each element. If the two matrices are of different
89sizes, then the compare has to be able to raise an exception to signal
90the error.
91
92In Python 2.1, rich comparisons were added in order to support this need.
93Python classes can now individually overload each of the \code{<},
94\code{<=}, \code{>}, \code{>=}, \code{==}, and \code{!=} operations.
95The new magic method names are:
96
97\begin{tableii}{c|l}{code}{Operation}{Method name}
98 \lineii{<}{\method{__lt__}}
99 \lineii{<=}{\method{__le__}}
100 \lineii{>}{\method{__gt__}}
101 \lineii{>=}{\method{__ge__}}
102 \lineii{==}{\method{__eq__}}
103 \lineii{!=}{\method{__ne__}}
104\end{tableii}
105
106(The magic methods are named after the corresponding Fortran operators
107\code{.LT.}. \code{.LE.}, \&c. Numeric programmers are almost
108certainly quite familar with these names and will find them easy to
109remember.)
110
111Each of these magic methods is of the form \code{\var{method}(self,
112other)}, where \code{self} will be the object on the left-hand side of
113the operator, while \code{other} will be the object on the right-hand
114side. For example, the expression \code{A < B} will cause
115\code{A.__lt__(B)} to be called.
116
117Each of these magic methods can return anything at all: a Boolean, a
118matrix, a list, or any other Python object. Alternatively they can
119raise an exception if the comparison is impossible, inconsistent, or
120otherwise meaningless.
121
122The built-in \function{cmp(A,B)} function can use the rich comparison
123machinery, and now accepts an optional argument specifying which
124comparison operation to use; this is given as one of the strings
125\code{"<"}, \code{"<="}, \code{">"}, \code{">="}, \code{"=="}, or
126\code{"!="}. If called without the optional third argument,
127\function{cmp()} will only return -1, 0, or +1 as in previous versions
128of Python; otherwise it will call the appropriate method and can
129return any Python object.
130
131There are also corresponding changes of interest to C programmers;
132there's a new slot \code{tp_richcmp} in type objects and an API for
133performing a given rich comparison. I won't cover the C API here, but
134will refer you to PEP 207, or the documentation for Python's C API,
135for the full list of related functions.
136
137\begin{seealso}
138
139\seepep{207}{Rich Comparisions}{Written by Guido van Rossum, heavily
140based on earlier work by David Ascher, and implemented by Guido van Rossum.}
141
142\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000143
144% ======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000145\section{PEP 230: Warning Framework}
146
147Over its 10 years of existence, Python has accumulated a certain
148number of obsolete modules and features along the way. It's difficult
149to know when a feature is safe to remove, since there's no way of
150knowing how much code uses it -- perhaps no programs depend on the
151feature, or perhaps many do. To enable removing old features in a
152more structured way, a warning framework was added. When the Python
153developers want to get rid of a feature, it will first trigger a
154warning in the next version of Python. The following Python version
155can then drop the feature, and users will have had a full release
156cycle to remove uses of the old feature.
157
158Python 2.1 adds the warning framework to be used in this scheme. It
159adds a \module{warnings} module that provide functions to issue
160warnings, and to filter out warnings that you don't want to be
161displayed. Third-party modules can also use this framework to
162deprecate old features that they no longer wish to support.
163
164For example, in Python 2.1 the \module{regex} module is deprecated,
165so importing it causes a warning to be printed:
166
167\begin{verbatim}
168>>> import regex
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000169__main__:1: DeprecationWarning: the regex module is deprecated; please use the re module
170>>>
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000171\end{verbatim}
172
173Warnings can be issued by calling the \function{warnings.warn} function:
174
175\begin{verbatim}
176warnings.warn("feature X no longer supported")
177\end{verbatim}
178
179The first parameter is the warning message; an additional optional
180parameters can be used to specify a particular warning category.
181
182Filters can be added to disable certain warnings; a regular expression
183pattern can be applied to the message or to the module name in order
184to suppress a warning. For example, you may have a program that uses
185the \module{regex} module and not want to spare the time to convert it
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000186to use the \module{re} module right now. The warning can be
187suppressed by calling
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000188
189\begin{verbatim}
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000190import warnings
191warnings.filterwarnings(action = 'ignore',
192 message='.*regex module is deprecated',
193 category=DeprecationWarning,
194 module = '__main__')
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000195\end{verbatim}
196
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000197This adds a filter that will apply only to warnings of the class
198\class{DeprecationWarning} triggered in the \module{__main__} module, and applies a regular expression to only match the message about the \module{regex} module being deprecated, and will cause such warnings to be ignored. Warnings can also be printed only once, printed every time the offending code is executed, or turned into exceptions that will cause the program to stop (unless the exceptions are caught in the usual way, of course).
199
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000200Functions were also added to Python's C API for issuing warnings;
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000201refer to PEP 230 or to Python's API documentation for the details.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000202
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000203\begin{seealso}
204\seepep{5}{Guidelines for Language Evolution}{Written by Paul Prescod,
205to specify procedures to be followed when removing old features from
206Python. The policy described in this PEP hasn't been officially
207adopted, but the eventual policy probably won't be too different from
208Prescod's proposal.}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000209
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000210\seepep{230}{Warning Framework}{Written and implemented by Guido van Rossum.}
211\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000212
213% ======================================================================
214\section{PEP 229: New Build System}
215
216When compiling Python, the user had to go in and edit the
217\file{Modules/Setup} file in order to enable various additional
218modules; the default set is relatively small and limited to modules
219that compile on most Unix platforms. This means that on Unix
220platforms with many more features, most notably Linux, Python
221installations often don't contain all useful modules they could.
222
223Python 2.0 added the Distutils, a set of modules for distributing and
224installing extensions. In Python 2.1, the Distutils are used to
225compile much of the standard library of extension modules,
226autodetecting which ones are supported on the current machine.
227It's hoped that this will make Python installations easier and more featureful.
228
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000229\begin{seealso}
230\seepep{229}{Using Distutils to Build Python}{Written and implemented by A.M. Kuchling.}
231\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000232
233% ======================================================================
234\section{PEP 217: Interactive Display Hook}
235
236When using the Python interpreter interactively, the output of
237commands is displayed using the built-in \function{repr()} function.
238In Python 2.1, the variable \module{sys.displayhook} can be set to a
239callable object which will be called instead of \function{repr()}.
240For example, you can set it to a special pretty-printing function:
241
242\begin{verbatim}
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000243>>> # Create a recursive data structure
244... L = [1,2,3]
245>>> L.append(L)
246>>> L # Show Python's default output
247[1, 2, 3, [...]]
248>>> # Use pprint.pprint() as the display function
249... import sys, pprint
250>>> sys.displayhook = pprint.pprint
251>>> L
252[1, 2, 3, <Recursion on list with id=135143996>]
253>>>
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000254\end{verbatim}
255
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000256\begin{seealso}
257
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000258\seepep{217}{Display Hook for Interactive Use}{Written and implemented by Moshe Zadka.}
259
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000260\end{seealso}
261
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000262% ======================================================================
263\section{PEP 208: New Coercion Model}
264
265How numeric coercion is done at the C level was significantly
266modified. This will only affect the authors of C extensions to
267Python, allowing them more flexibility in writing extension types that
268support numeric operations.
269
270Extension types can now set the type flag
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000271\code{Py_TPFLAGS_CHECKTYPES} in their \code{PyTypeObject}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000272structure to indicate that they support the new coercion model. In
273such extension types, the numeric slot functions can no longer assume
274that they'll be passed two arguments of the same type; instead they
275may be passed two arguments of differing types, and can then perform
276their own internal coercion. If the slot function is passed a type it
277can't handle, it can indicate the failure by returning a reference to
278the \code{Py_NotImplemented} singleton value. The numeric functions
279of the other type will then be tried, and perhaps they can handle the
280operation; if the other type also returns \code{Py_NotImplemented},
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000281then a \exception{TypeError} will be raised. Numeric methods written
282in Python can also return \code{Py_NotImplemented}, causing the
283interpreter to act as if the method did not exist (perhaps raising a
284\exception{TypeError}, perhaps trying another object's numeric
285methods).
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000286
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000287\begin{seealso}
288
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000289\seepep{208}{Reworking the Coercion Model}{Written and implemented by
290Neil Schemenauer, heavily based upon earlier work by Marc-Andr\'e
291Lemburg. Read this to understand the fine points of how numeric
292operations will now be processed at the C level.}
293
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000294\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000295
296% ======================================================================
297\section{Minor Changes and Fixes}
298
299There were relatively few smaller changes made in Python 2.1 due to
300the shorter release cycle. A search through the CVS change logs turns
301up 57 patches applied, and 86 bugs fixed; both figures are likely to
302be underestimates. Some of the more notable changes are:
303
304\begin{itemize}
305
306
307\item The speed of line-oriented file I/O has been improved because
308people often complain about its lack of speed, and because it's often
309been used as a na\"ive benchmark. The \method{readline()} method of
310file objects has therefore been rewritten to be much faster. The
311exact amount of the speedup will vary from platform to platform
312depending on how slow the C library's \function{getc()} was, but is
313around 66\%, and potentially much faster on some particular operating
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000314systems. Tim Peters did much of the benchmarking and coding for this
315change, motivated by a discussion in comp.lang.python.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000316
317A new module and method for file objects was also added, contributed
318by Jeff Epler. The new method, \method{xreadlines()}, is similar to
319the existing \function{xrange()} built-in. \function{xreadlines()}
320returns an opaque sequence object that only supports being iterated
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000321over, reading a line on every iteration but not reading the entire
322file into memory as the existing \method{readline()} method. You'd
323use it like this:
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000324
325\begin{verbatim}
326for line in sys.stdin.xreadlines():
327 # ... do something for each line ...
328 ...
329\end{verbatim}
330
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000331For a fuller discussion of the line I/O changes, see the python-dev
332summary for January 1-15, 2001.
Andrew M. Kuchling91834c62001-01-22 19:51:13 +0000333
334\item A new method, \method{popitem()}, was added to dictionaries to enable
335destructively iterating through the contents of a dictionary; this can be faster for large dictionaries because .
336\code{D.popitem()} removes a random \code{(\var{key}, \var{value})} pair
337from the dictionary and returns it as a 2-tuple. This was implemented
338mostly by Tim Peters and Guido van Rossum, after a suggestion and
339preliminary patch by Moshe Zadka.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000340
341\item \module{curses.panel}, a wrapper for the panel library, part of
342ncurses and of SYSV curses, was contributed by Thomas Gellekum. The
343panel library provides windows with the additional feature of depth.
344Windows can be moved higher or lower in the depth ordering, and the
345panel library figures out where panels overlap and which sections are
346visible.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000347
348\item Modules can now control which names are imported when \code{from
349\var{module} import *} is used, by defining a \code{__all__} attribute
350containing a list of names that will be imported. One common
351complaint is that if the module imports other modules such as
352\module{sys} or \module{string}, \code{from \var{module} import *}
353will add them to the importing module's namespace. To fix this,
354simply list the public names in \code{__all__}:
355
356\begin{verbatim}
357# List public names
358__all__ = ['Database', 'open']
359\end{verbatim}
360
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000361A stricter version of this patch was first suggested and implemented
Andrew M. Kuchling91834c62001-01-22 19:51:13 +0000362by Ben Wolfson, but after some python-dev discussion, a weaker
363final version was checked in.
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000364
Andrew M. Kuchling91834c62001-01-22 19:51:13 +0000365\item The PyXML package has gone through a few releases since Python
3662.0, and Python 2.1 includes an updated version of the \module{xml}
367package. Some of the noteworthy changes include support for Expat
3681.2, the ability for Expat parsers to handle files in any encoding
369supported by Python, and various bugfixes for SAX, DOM, and the
370\module{minidom} module.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000371
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000372\item Various functions in the \module{time} module, such as
373\function{asctime()} and \function{localtime()},
374require a floating point argument containing the time in seconds since
375the epoch. The most common use of these functions is to work with the
376current time, so the floating point argument has been made optional;
377when a value isn't provided, the current time will be used. For
378example, log file entries usually need a string containing the current
379time; in Python 2.1, \code{time.asctime()} can be used, instead of the
380lengthier \code{time.asctime(time.localtime(time.time()))} that was
381previously required.
382
383This change was proposed and implemented by Thomas Wouters.
384
Andrew M. Kuchling91834c62001-01-22 19:51:13 +0000385\item The \module{ftplib} module now defaults to retrieving files in passive mode,
386because passive mode is more likely to work from behind a firewall.
387This request came from the Debian bug tracking system, since other
388Debian packages use \module{ftplib} to retrieve files and then don't
389work from behind a firewall. It's deemed unlikely that this will
390cause problems for anyone, because Netscape defaults to passive mode
391and few people complain, but if passive mode is unsuitable for your
392application or network setup, call
393\method{set_pasv(0)} on FTP objects to disable passive mode.
394
395\item The size of the Unicode character database was compressed by another 55K thanks to Fredrik Lundh.
396
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000397\end{itemize}
398
399And there's the usual list of bugfixes, minor memory leaks, docstring
400edits, and other tweaks, too lengthy to be worth itemizing; see the
401CVS logs for the full details if you want them.
402
403
404% ======================================================================
Andrew M. Kuchling91834c62001-01-22 19:51:13 +0000405\section{XXX Nested Scopes ?}
406
407xxx
408
409% ======================================================================
410\section{XXX Weak References ?}
411
412xxx
413
414% ======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000415\section{Acknowledgements}
416
417The author would like to thank the following people for offering
Andrew M. Kuchling91834c62001-01-22 19:51:13 +0000418suggestions on various drafts of this article: Michael Hudson,
419Marc-Andr\'e Lemburg,
420Neil Schemenauer, Thomas Wouters.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000421
422\end{document}