blob: 88a45f9f14c756a0a6378067f708596d0f3c44ef [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}
6\release{0.01}
7\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% ======================================================================
145\section{XXX Nested Scopes ?}
146
147xxx
148
149% ======================================================================
150\section{PEP 230: Warning Framework}
151
152Over its 10 years of existence, Python has accumulated a certain
153number of obsolete modules and features along the way. It's difficult
154to know when a feature is safe to remove, since there's no way of
155knowing how much code uses it -- perhaps no programs depend on the
156feature, or perhaps many do. To enable removing old features in a
157more structured way, a warning framework was added. When the Python
158developers want to get rid of a feature, it will first trigger a
159warning in the next version of Python. The following Python version
160can then drop the feature, and users will have had a full release
161cycle to remove uses of the old feature.
162
163Python 2.1 adds the warning framework to be used in this scheme. It
164adds a \module{warnings} module that provide functions to issue
165warnings, and to filter out warnings that you don't want to be
166displayed. Third-party modules can also use this framework to
167deprecate old features that they no longer wish to support.
168
169For example, in Python 2.1 the \module{regex} module is deprecated,
170so importing it causes a warning to be printed:
171
172\begin{verbatim}
173>>> import regex
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000174__main__:1: DeprecationWarning: the regex module is deprecated; please use the re module
175>>>
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000176\end{verbatim}
177
178Warnings can be issued by calling the \function{warnings.warn} function:
179
180\begin{verbatim}
181warnings.warn("feature X no longer supported")
182\end{verbatim}
183
184The first parameter is the warning message; an additional optional
185parameters can be used to specify a particular warning category.
186
187Filters can be added to disable certain warnings; a regular expression
188pattern can be applied to the message or to the module name in order
189to suppress a warning. For example, you may have a program that uses
190the \module{regex} module and not want to spare the time to convert it
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000191to use the \module{re} module right now. The warning can be
192suppressed by calling
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000193
194\begin{verbatim}
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000195import warnings
196warnings.filterwarnings(action = 'ignore',
197 message='.*regex module is deprecated',
198 category=DeprecationWarning,
199 module = '__main__')
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000200\end{verbatim}
201
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000202This adds a filter that will apply only to warnings of the class
203\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).
204
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000205Functions were also added to Python's C API for issuing warnings;
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000206refer to PEP 230 or to Python's API documentation for the details.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000207
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000208\begin{seealso}
209\seepep{5}{Guidelines for Language Evolution}{Written by Paul Prescod,
210to specify procedures to be followed when removing old features from
211Python. The policy described in this PEP hasn't been officially
212adopted, but the eventual policy probably won't be too different from
213Prescod's proposal.}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000214
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000215\seepep{230}{Warning Framework}{Written and implemented by Guido van Rossum.}
216\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000217
218% ======================================================================
219\section{PEP 229: New Build System}
220
221When compiling Python, the user had to go in and edit the
222\file{Modules/Setup} file in order to enable various additional
223modules; the default set is relatively small and limited to modules
224that compile on most Unix platforms. This means that on Unix
225platforms with many more features, most notably Linux, Python
226installations often don't contain all useful modules they could.
227
228Python 2.0 added the Distutils, a set of modules for distributing and
229installing extensions. In Python 2.1, the Distutils are used to
230compile much of the standard library of extension modules,
231autodetecting which ones are supported on the current machine.
232It's hoped that this will make Python installations easier and more featureful.
233
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000234\begin{seealso}
235\seepep{229}{Using Distutils to Build Python}{Written and implemented by A.M. Kuchling.}
236\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000237
238% ======================================================================
239\section{PEP 217: Interactive Display Hook}
240
241When using the Python interpreter interactively, the output of
242commands is displayed using the built-in \function{repr()} function.
243In Python 2.1, the variable \module{sys.displayhook} can be set to a
244callable object which will be called instead of \function{repr()}.
245For example, you can set it to a special pretty-printing function:
246
247\begin{verbatim}
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000248>>> # Create a recursive data structure
249... L = [1,2,3]
250>>> L.append(L)
251>>> L # Show Python's default output
252[1, 2, 3, [...]]
253>>> # Use pprint.pprint() as the display function
254... import sys, pprint
255>>> sys.displayhook = pprint.pprint
256>>> L
257[1, 2, 3, <Recursion on list with id=135143996>]
258>>>
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000259\end{verbatim}
260
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000261\begin{seealso}
262
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000263\seepep{217}{Display Hook for Interactive Use}{Written and implemented by Moshe Zadka.}
264
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000265\end{seealso}
266
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000267% ======================================================================
268\section{PEP 208: New Coercion Model}
269
270How numeric coercion is done at the C level was significantly
271modified. This will only affect the authors of C extensions to
272Python, allowing them more flexibility in writing extension types that
273support numeric operations.
274
275Extension types can now set the type flag
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000276\code{Py_TPFLAGS_CHECKTYPES} in their \code{PyTypeObject}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000277structure to indicate that they support the new coercion model. In
278such extension types, the numeric slot functions can no longer assume
279that they'll be passed two arguments of the same type; instead they
280may be passed two arguments of differing types, and can then perform
281their own internal coercion. If the slot function is passed a type it
282can't handle, it can indicate the failure by returning a reference to
283the \code{Py_NotImplemented} singleton value. The numeric functions
284of the other type will then be tried, and perhaps they can handle the
285operation; if the other type also returns \code{Py_NotImplemented},
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000286then a \exception{TypeError} will be raised. Numeric methods written
287in Python can also return \code{Py_NotImplemented}, causing the
288interpreter to act as if the method did not exist (perhaps raising a
289\exception{TypeError}, perhaps trying another object's numeric
290methods).
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000291
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000292\begin{seealso}
293
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000294\seepep{208}{Reworking the Coercion Model}{Written and implemented by
295Neil Schemenauer, heavily based upon earlier work by Marc-Andr\'e
296Lemburg. Read this to understand the fine points of how numeric
297operations will now be processed at the C level.}
298
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000299\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000300
301% ======================================================================
302\section{Minor Changes and Fixes}
303
304There were relatively few smaller changes made in Python 2.1 due to
305the shorter release cycle. A search through the CVS change logs turns
306up 57 patches applied, and 86 bugs fixed; both figures are likely to
307be underestimates. Some of the more notable changes are:
308
309\begin{itemize}
310
311
312\item The speed of line-oriented file I/O has been improved because
313people often complain about its lack of speed, and because it's often
314been used as a na\"ive benchmark. The \method{readline()} method of
315file objects has therefore been rewritten to be much faster. The
316exact amount of the speedup will vary from platform to platform
317depending on how slow the C library's \function{getc()} was, but is
318around 66\%, and potentially much faster on some particular operating
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000319systems. Tim Peters did much of the benchmarking and coding for this
320change, motivated by a discussion in comp.lang.python.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000321
322A new module and method for file objects was also added, contributed
323by Jeff Epler. The new method, \method{xreadlines()}, is similar to
324the existing \function{xrange()} built-in. \function{xreadlines()}
325returns an opaque sequence object that only supports being iterated
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000326over, reading a line on every iteration but not reading the entire
327file into memory as the existing \method{readline()} method. You'd
328use it like this:
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000329
330\begin{verbatim}
331for line in sys.stdin.xreadlines():
332 # ... do something for each line ...
333 ...
334\end{verbatim}
335
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000336For a fuller discussion of the line I/O changes, see the python-dev
337summary for January 1-15, 2001.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000338
339\item \module{curses.panel}, a wrapper for the panel library, part of
340ncurses and of SYSV curses, was contributed by Thomas Gellekum. The
341panel library provides windows with the additional feature of depth.
342Windows can be moved higher or lower in the depth ordering, and the
343panel library figures out where panels overlap and which sections are
344visible.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000345
346\item Modules can now control which names are imported when \code{from
347\var{module} import *} is used, by defining a \code{__all__} attribute
348containing a list of names that will be imported. One common
349complaint is that if the module imports other modules such as
350\module{sys} or \module{string}, \code{from \var{module} import *}
351will add them to the importing module's namespace. To fix this,
352simply list the public names in \code{__all__}:
353
354\begin{verbatim}
355# List public names
356__all__ = ['Database', 'open']
357\end{verbatim}
358
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000359A stricter version of this patch was first suggested and implemented
360by Ben Wolfson, but after some python-dev discussion, this weaker final version
361
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000362\item The \module{ftplib} module now defaults to retrieving files in passive mode,
363because passive mode is more likely to work from behind a firewall.
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000364This request came from the Debian bug tracking system, since other
365Debian packages use \module{ftplib} to retrieve files and then don't
366work from behind a firewall. It's deemed unlikely that this will
367cause problems for anyone, because Netscape defaults to passive mode
368and few people complain, but if passive mode is unsuitable for your
369application or network setup, call
370\method{set_pasv(0)} on FTP objects to disable passive mode.
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. Kuchling90cecee2001-01-22 04:02:09 +0000385\end{itemize}
386
387And there's the usual list of bugfixes, minor memory leaks, docstring
388edits, and other tweaks, too lengthy to be worth itemizing; see the
389CVS logs for the full details if you want them.
390
391
392% ======================================================================
393\section{Acknowledgements}
394
395The author would like to thank the following people for offering
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000396suggestions on various drafts of this article: Neil Schemenauer,
397Thomas Wouters.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000398
399\end{document}