blob: 7ce734e70fa4ec9a6eba01a9bc2f00221709f34a [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. Kuchling6a360bd2001-02-05 02:47:52 +00006\release{0.05}
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
16for Python 2.1 alpha 2. Please send any comments, bug reports, or
17questions, 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
37Currently 2.1 is available in an alpha release, but the release
38schedule calls for a beta release by late February 2001, and a final
39release in April 2001.
40
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +000041%======================================================================
42\section{PEP 227: Nested Scopes}
43
44The largest change in Python 2.1 is to Python's scoping rules. In
45Python 2.0, at any given time there are at most three namespaces used
46to look up variable names: local, module-level, and the built-in
47namespace. This often surprised people because it didn't match their
48intuitive expectations. For example, a nested recursive function
49definition doesn't work:
50
51\begin{verbatim}
52def f():
53 ...
54 def g(value):
55 ...
56 return g(value-1) + 1
57 ...
58\end{verbatim}
59
60The function \function{g()} will always raise a \exception{NameError}
61exception, because the binding of the name \samp{g} isn't in either
62its local namespace or in the module-level namespace. This isn't much
63of a problem in practice (how often do you recursively define interior
64functions like this?), but this also made using the \keyword{lambda}
65statement clumsier, and this was a problem in practice. In code which
66uses \keyword{lambda} you can often find local variables being copied
67by passing them as the default values of arguments.
68
69\begin{verbatim}
70def find(self, name):
71 "Return list of any entries equal to 'name'"
72 L = filter(lambda x, name=name: x == name,
73 self.list_attribute)
74 return L
75\end{verbatim}
76
77The readability of Python code written in a strongly functional style
78suffers greatly as a result.
79
80The most significant change to Python 2.1 is that static scoping has
81been added to the language to fix this problem. As a first effect,
82the \code{name=name} default argument is now unnecessary in the above
83example. Put simply, when a given variable name is not assigned a
84value within a function (by an assignment, or the \keyword{def},
85\keyword{class}, or \keyword{import} statements), references to the
86variable will be looked up in the local namespace of the enclosing
87scope. A more detailed explanation of the rules, and a dissection of
88the implementation, can be found in the PEP.
89
90This change may cause some compatibility problems for code where the
91same variable name is used both at the module level and as a local
92variable within a function that contains further function definitions.
93This seems rather unlikely though, since such code would have been
94pretty confusing to read in the first place.
95
96One side effect of the change is that the statement from \code{from
97\var{module} import *} has been made illegal inside a function scope.
98The Python reference manual has said all along that \code{from
99\var{module} import *} is only legal at the top level of a module, but
100the CPython interpreter has never enforced this before; it will be
101enforced in 2.1, though it's not yet clear if it will be a syntax
102error or just a warning. In the alpha 2 release, it triggers a
103\exception{SyntaxError} exception, but this check might be made more
104lenient in following releases.
105% XXX update the previous sentence for 2.1final
106
107\begin{seealso}
108
109\seepep{227}{Statically Nested Scopes}{Written and implemented by
110Jeremy Hylton.}
111
112\end{seealso}
113
114
115%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000116\section{PEP 232: Function Attributes}
117
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000118In Python 2.1, functions can now have arbitrary information attached
119to them. People were often using docstrings to hold information about
120functions and methods, because the \code{__doc__} attribute was the
121only way of attaching any information to a function. For example, in
122the Zope Web application server, functions are marked as safe for
123public access by having a docstring, and in John Aycock's SPARK
124parsing framework, docstrings hold parts of the BNF grammar to be
125parsed. This overloading is unfortunate, since docstrings are really
126intended to hold a function's documentation, and it means you can't
127properly document functions intended for private use in Zope.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000128
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000129Attributes can now be set and retrieved on functions, using the
130regular Python syntax:
131
132\begin{verbatim}
133def f(): pass
134
135f.publish = 1
136f.secure = 1
137f.grammar = "A ::= B (C D)*"
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000138\end{verbatim}
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000139
140The dictionary containing attributes can be accessed as
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000141\member{__dict__}. Unlike the \member{__dict__} attribute of class
142instances, in functions you can actually assign a new dictionary to
143\member{__dict__}, though the new value is restricted to a regular
144Python dictionary; you can't be tricky and set it to a
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000145\class{UserDict} instance, a DBM file, or any other random mapping
146object.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000147
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000148\begin{seealso}
149
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000150\seepep{232}{Function Attributes}{Written and implemented by Barry
151Warsaw.}
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000152
153\end{seealso}
154
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000155%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000156\section{PEP 207: Rich Comparisons}
157
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000158In earlier versions, Python's support for implementing comparisons on
159user-defined classes and extension types was quite simple. Classes
160could implement a \method{__cmp__} method that was given two instances
161of a class, and could only return 0 if they were equal or +1 or -1 if
162they weren't; the method couldn't raise an exception or return
163anything other than a Boolean value. Users of Numeric Python often
164found this model too weak and restrictive, because in the
165number-crunching programs that numeric Python is used for, it would be
166more useful to be able to perform elementwise comparisons of two
167matrices, returning a matrix containing the results of a given
168comparison for each element. If the two matrices are of different
169sizes, then the compare has to be able to raise an exception to signal
170the error.
171
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000172In Python 2.1, rich comparisons were added in order to support this
173need. Python classes can now individually overload each of the
174\code{<}, \code{<=}, \code{>}, \code{>=}, \code{==}, and \code{!=}
175operations. The new magic method names are:
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000176
177\begin{tableii}{c|l}{code}{Operation}{Method name}
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000178 \lineii{<}{\method{__lt__}} \lineii{<=}{\method{__le__}}
179 \lineii{>}{\method{__gt__}} \lineii{>=}{\method{__ge__}}
180 \lineii{==}{\method{__eq__}} \lineii{!=}{\method{__ne__}}
181 \end{tableii}
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000182
183(The magic methods are named after the corresponding Fortran operators
184\code{.LT.}. \code{.LE.}, \&c. Numeric programmers are almost
185certainly quite familar with these names and will find them easy to
186remember.)
187
188Each of these magic methods is of the form \code{\var{method}(self,
189other)}, where \code{self} will be the object on the left-hand side of
190the operator, while \code{other} will be the object on the right-hand
191side. For example, the expression \code{A < B} will cause
192\code{A.__lt__(B)} to be called.
193
194Each of these magic methods can return anything at all: a Boolean, a
195matrix, a list, or any other Python object. Alternatively they can
196raise an exception if the comparison is impossible, inconsistent, or
197otherwise meaningless.
198
199The built-in \function{cmp(A,B)} function can use the rich comparison
200machinery, and now accepts an optional argument specifying which
201comparison operation to use; this is given as one of the strings
202\code{"<"}, \code{"<="}, \code{">"}, \code{">="}, \code{"=="}, or
203\code{"!="}. If called without the optional third argument,
204\function{cmp()} will only return -1, 0, or +1 as in previous versions
205of Python; otherwise it will call the appropriate method and can
206return any Python object.
207
208There are also corresponding changes of interest to C programmers;
209there's a new slot \code{tp_richcmp} in type objects and an API for
210performing a given rich comparison. I won't cover the C API here, but
211will refer you to PEP 207, or the documentation for Python's C API,
212for the full list of related functions.
213
214\begin{seealso}
215
216\seepep{207}{Rich Comparisions}{Written by Guido van Rossum, heavily
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000217based on earlier work by David Ascher, and implemented by Guido van
218Rossum.}
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000219
220\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000221
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000222%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000223\section{PEP 230: Warning Framework}
224
225Over its 10 years of existence, Python has accumulated a certain
226number of obsolete modules and features along the way. It's difficult
227to know when a feature is safe to remove, since there's no way of
Andrew M. Kuchlingf33c1182001-01-23 02:48:26 +0000228knowing how much code uses it --- perhaps no programs depend on the
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000229feature, or perhaps many do. To enable removing old features in a
230more structured way, a warning framework was added. When the Python
231developers want to get rid of a feature, it will first trigger a
232warning in the next version of Python. The following Python version
233can then drop the feature, and users will have had a full release
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000234cycle to remove uses of the old feature.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000235
236Python 2.1 adds the warning framework to be used in this scheme. It
237adds a \module{warnings} module that provide functions to issue
238warnings, and to filter out warnings that you don't want to be
239displayed. Third-party modules can also use this framework to
240deprecate old features that they no longer wish to support.
241
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000242For example, in Python 2.1 the \module{regex} module is deprecated, so
243importing it causes a warning to be printed:
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000244
245\begin{verbatim}
246>>> import regex
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000247__main__:1: DeprecationWarning: the regex module
248 is deprecated; please use the re module
249>>>
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000250\end{verbatim}
251
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000252Warnings can be issued by calling the \function{warnings.warn}
253function:
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000254
255\begin{verbatim}
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000256warnings.warn("feature X no longer supported")
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000257\end{verbatim}
258
259The first parameter is the warning message; an additional optional
260parameters can be used to specify a particular warning category.
261
262Filters can be added to disable certain warnings; a regular expression
263pattern can be applied to the message or to the module name in order
264to suppress a warning. For example, you may have a program that uses
265the \module{regex} module and not want to spare the time to convert it
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000266to use the \module{re} module right now. The warning can be
267suppressed by calling
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000268
269\begin{verbatim}
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000270import warnings
271warnings.filterwarnings(action = 'ignore',
272 message='.*regex module is deprecated',
273 category=DeprecationWarning,
274 module = '__main__')
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000275\end{verbatim}
276
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000277This adds a filter that will apply only to warnings of the class
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000278\class{DeprecationWarning} triggered in the \module{__main__} module,
279and applies a regular expression to only match the message about the
280\module{regex} module being deprecated, and will cause such warnings
281to be ignored. Warnings can also be printed only once, printed every
282time the offending code is executed, or turned into exceptions that
283will cause the program to stop (unless the exceptions are caught in
284the usual way, of course).
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000285
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000286Functions were also added to Python's C API for issuing warnings;
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000287refer to PEP 230 or to Python's API documentation for the details.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000288
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000289\begin{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000290
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000291\seepep{5}{Guidelines for Language Evolution}{Written
292by Paul Prescod, to specify procedures to be followed when removing
293old features from Python. The policy described in this PEP hasn't
294been officially adopted, but the eventual policy probably won't be too
295different from Prescod's proposal.}
296
297\seepep{230}{Warning Framework}{Written and implemented by Guido van
298Rossum.}
299
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000300\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000301
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000302%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000303\section{PEP 229: New Build System}
304
305When compiling Python, the user had to go in and edit the
306\file{Modules/Setup} file in order to enable various additional
307modules; the default set is relatively small and limited to modules
308that compile on most Unix platforms. This means that on Unix
309platforms with many more features, most notably Linux, Python
310installations often don't contain all useful modules they could.
311
312Python 2.0 added the Distutils, a set of modules for distributing and
313installing extensions. In Python 2.1, the Distutils are used to
314compile much of the standard library of extension modules,
Andrew M. Kuchlingf33c1182001-01-23 02:48:26 +0000315autodetecting which ones are supported on the current machine. It's
316hoped that this will make Python installations easier and more
317featureful.
318
319Instead of having to edit the \file{Modules/Setup} file in order to
320enable modules, a \file{setup.py} script in the top directory of the
321Python source distribution is run at build time, and attempts to
322discover which modules can be enabled by examining the modules and
323header files on the system. In 2.1alpha1, there's very little you can
324do to change \file{setup.py}'s behaviour, or to discover why a given
325module isn't compiled. If you run into problems in 2.1alpha1, please
326report them, and be prepared to dive into \file{setup.py} in order to
327fix autodetection of a given library on your system. In the alpha2
328release I plan to add ways to have more control over what the script
329does (probably command-line arguments to \file{configure} or to
330\file{setup.py}).
331
332If it turns out to be impossible to make autodetection work reliably,
333it's possible that this change may become an optional build method
334instead of the default, or it may even be backed out completely.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000335
Andrew M. Kuchling4308d3c2001-01-29 17:36:53 +0000336In another far-reaching change to the build mechanism, Neil
337Schemenauer restructured things so Python now uses a single makefile
338that isn't recursive, instead of makefiles in the top directory and in
339each of the Python/, Parser/, Objects/, and Modules/ subdirectories.
340This makes building Python faster, and also makes the build process
341clearer and simpler.
342
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000343\begin{seealso}
344
345\seepep{229}{Using Distutils to Build Python}{Written
346and implemented by A.M. Kuchling.}
347
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000348\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000349
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000350%======================================================================
351\section{Weak References}
352
353Weak references are a minor but useful new data type in the Python
354programmer's toolbox. Storing a reference to an object (say, in a
355dictionary or a list) has the side effect of keeping that object alive
356forever. There are a few specific cases where this behaviour is
357undesirable, object caches being the most common one, and another
358being circular references in data structures such as trees.
359
360For example, a tree might be implemented as a set of \class{Node}
361instances where each instances contains a list of its children. If
362you need to be able to determine the parent of a given \class{Node},
363an obvious solution would be to have each instance have a reference to
364its parent. This creates lots of circular references.
365
366XXX finish the rest of this section
367
368\begin{seealso}
369
370\seepep{205}{Weak References}{Written and implemented by
371Fred~L. Drake,~Jr.}
372
373\end{seealso}
374
375%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000376\section{PEP 217: Interactive Display Hook}
377
378When using the Python interpreter interactively, the output of
379commands is displayed using the built-in \function{repr()} function.
380In Python 2.1, the variable \module{sys.displayhook} can be set to a
381callable object which will be called instead of \function{repr()}.
382For example, you can set it to a special pretty-printing function:
383
384\begin{verbatim}
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000385>>> # Create a recursive data structure
386... L = [1,2,3]
387>>> L.append(L)
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000388>>> L # Show Python's default output
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000389[1, 2, 3, [...]]
390>>> # Use pprint.pprint() as the display function
391... import sys, pprint
392>>> sys.displayhook = pprint.pprint
393>>> L
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000394[1, 2, 3, <Recursion on list with id=135143996>]
395>>>
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000396\end{verbatim}
397
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000398\begin{seealso}
399
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000400\seepep{217}{Display Hook for Interactive Use}{Written and implemented
401by Moshe Zadka.}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000402
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000403\end{seealso}
404
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000405%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000406\section{PEP 208: New Coercion Model}
407
408How numeric coercion is done at the C level was significantly
409modified. This will only affect the authors of C extensions to
410Python, allowing them more flexibility in writing extension types that
411support numeric operations.
412
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000413Extension types can now set the type flag \code{Py_TPFLAGS_CHECKTYPES}
414in their \code{PyTypeObject} structure to indicate that they support
415the new coercion model. In such extension types, the numeric slot
416functions can no longer assume that they'll be passed two arguments of
417the same type; instead they may be passed two arguments of differing
418types, and can then perform their own internal coercion. If the slot
419function is passed a type it can't handle, it can indicate the failure
420by returning a reference to the \code{Py_NotImplemented} singleton
421value. The numeric functions of the other type will then be tried,
422and perhaps they can handle the operation; if the other type also
423returns \code{Py_NotImplemented}, then a \exception{TypeError} will be
424raised. Numeric methods written in Python can also return
425\code{Py_NotImplemented}, causing the interpreter to act as if the
426method did not exist (perhaps raising a \exception{TypeError}, perhaps
427trying another object's numeric methods).
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000428
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000429\begin{seealso}
430
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000431\seepep{208}{Reworking the Coercion Model}{Written and implemented by
432Neil Schemenauer, heavily based upon earlier work by Marc-Andr\'e
433Lemburg. Read this to understand the fine points of how numeric
434operations will now be processed at the C level.}
435
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000436\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000437
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000438%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000439\section{Minor Changes and Fixes}
440
441There were relatively few smaller changes made in Python 2.1 due to
442the shorter release cycle. A search through the CVS change logs turns
443up 57 patches applied, and 86 bugs fixed; both figures are likely to
444be underestimates. Some of the more notable changes are:
445
446\begin{itemize}
447
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000448\item The speed of line-oriented file I/O has been improved because
449people often complain about its lack of speed, and because it's often
450been used as a na\"ive benchmark. The \method{readline()} method of
451file objects has therefore been rewritten to be much faster. The
452exact amount of the speedup will vary from platform to platform
453depending on how slow the C library's \function{getc()} was, but is
454around 66\%, and potentially much faster on some particular operating
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000455systems. Tim Peters did much of the benchmarking and coding for this
456change, motivated by a discussion in comp.lang.python.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000457
458A new module and method for file objects was also added, contributed
459by Jeff Epler. The new method, \method{xreadlines()}, is similar to
460the existing \function{xrange()} built-in. \function{xreadlines()}
461returns an opaque sequence object that only supports being iterated
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000462over, reading a line on every iteration but not reading the entire
Andrew M. Kuchlingf33c1182001-01-23 02:48:26 +0000463file into memory as the existing \method{readlines()} method does.
464You'd use it like this:
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000465
466\begin{verbatim}
467for line in sys.stdin.xreadlines():
468 # ... do something for each line ...
469 ...
470\end{verbatim}
471
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000472For a fuller discussion of the line I/O changes, see the python-dev
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000473summary for January 1-15, 2001.
Andrew M. Kuchling91834c62001-01-22 19:51:13 +0000474
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000475\item A new method, \method{popitem()}, was added to dictionaries to
476enable destructively iterating through the contents of a dictionary;
477this can be faster for large dictionaries because .
478\code{D.popitem()} removes a random \code{(\var{key}, \var{value})}
479pair from the dictionary and returns it as a 2-tuple. This was
480implemented mostly by Tim Peters and Guido van Rossum, after a
481suggestion and preliminary patch by Moshe Zadka.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000482
483\item \module{curses.panel}, a wrapper for the panel library, part of
484ncurses and of SYSV curses, was contributed by Thomas Gellekum. The
485panel library provides windows with the additional feature of depth.
486Windows can be moved higher or lower in the depth ordering, and the
487panel library figures out where panels overlap and which sections are
488visible.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000489
490\item Modules can now control which names are imported when \code{from
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000491\var{module} import *} is used, by defining an \code{__all__}
492attribute containing a list of names that will be imported. One
493common complaint is that if the module imports other modules such as
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000494\module{sys} or \module{string}, \code{from \var{module} import *}
495will add them to the importing module's namespace. To fix this,
496simply list the public names in \code{__all__}:
497
498\begin{verbatim}
499# List public names
500__all__ = ['Database', 'open']
501\end{verbatim}
502
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000503A stricter version of this patch was first suggested and implemented
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000504by Ben Wolfson, but after some python-dev discussion, a weaker final
505version was checked in.
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000506
Andrew M. Kuchling91834c62001-01-22 19:51:13 +0000507\item The PyXML package has gone through a few releases since Python
5082.0, and Python 2.1 includes an updated version of the \module{xml}
509package. Some of the noteworthy changes include support for Expat
5101.2, the ability for Expat parsers to handle files in any encoding
511supported by Python, and various bugfixes for SAX, DOM, and the
512\module{minidom} module.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000513
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000514\item Various functions in the \module{time} module, such as
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000515\function{asctime()} and \function{localtime()}, require a floating
516point argument containing the time in seconds since the epoch. The
517most common use of these functions is to work with the current time,
518so the floating point argument has been made optional; when a value
519isn't provided, the current time will be used. For example, log file
520entries usually need a string containing the current time; in Python
5212.1, \code{time.asctime()} can be used, instead of the lengthier
522\code{time.asctime(time.localtime(time.time()))} that was previously
523required.
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000524
525This change was proposed and implemented by Thomas Wouters.
526
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000527\item Applying \function{repr()} to strings previously used octal
528escapes for non-printable characters; for example, a newline was
529\code{'\e 012'}. This was a vestigial trace of Python's C ancestry, but
530today octal is of very little practical use. Ka-Ping Yee suggested
531using hex escapes instead of octal ones, and using the \code{\e n},
532\code{\e t}, \code{\e r} escapes for the appropriate characters, and
533implemented this new formatting.
Andrew M. Kuchling4308d3c2001-01-29 17:36:53 +0000534
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000535\item The \module{ftplib} module now defaults to retrieving files in
536passive mode, because passive mode is more likely to work from behind
537a firewall. This request came from the Debian bug tracking system,
538since other Debian packages use \module{ftplib} to retrieve files and
539then don't work from behind a firewall. It's deemed unlikely that
540this will cause problems for anyone, because Netscape defaults to
541passive mode and few people complain, but if passive mode is
542unsuitable for your application or network setup, call
543\method{set_pasv(0)} on FTP objects to disable passive mode.
Andrew M. Kuchling91834c62001-01-22 19:51:13 +0000544
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000545\item Support for raw socket access has been added to the
546\module{socket} module, contributed by Grant Edwards.
547
548\item Syntax errors detected at compile-time can now raise exceptions
549containing the filename and line number of the error, a pleasant side
550effect of the compiler reorganization done by Jeremy Hylton.
551
552\item The size of the Unicode character database was shrunk by another
553340K thanks to Fredrik Lundh.
Andrew M. Kuchling91834c62001-01-22 19:51:13 +0000554
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000555\end{itemize}
556
557And there's the usual list of bugfixes, minor memory leaks, docstring
558edits, and other tweaks, too lengthy to be worth itemizing; see the
559CVS logs for the full details if you want them.
560
561
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000562%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000563\section{Acknowledgements}
564
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000565The author would like to thank the following people for offering
566suggestions on various drafts of this article: Graeme Cross, David
567Goodger, Jay Graves, Michael Hudson, Marc-Andr\'e Lemburg, Fredrik
568Lundh, Neil Schemenauer, Thomas Wouters.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000569
570\end{document}