blob: 32158b0e62a55cce4b4d3f4efae345e2de62a997 [file] [log] [blame]
Fred Drakeed0fa3d2003-07-30 19:14:09 +00001\documentclass{howto}
2\usepackage{distutils}
3% $Id$
4
5\title{What's New in Python 2.4}
6\release{0.0}
7\author{A.M.\ Kuchling}
8\authoraddress{\email{amk@amk.ca}}
9
10\begin{document}
11\maketitle
12\tableofcontents
13
14This article explains the new features in Python 2.4. No release date
15for Python 2.4 has been set; expect that this will happen in 2004.
16
17While Python 2.3 was primarily a library development release, Python
182.4 may extend the core language and interpreter in
19as-yet-undetermined ways.
20
21This article doesn't attempt to provide a complete specification of
22the new features, but instead provides a convenient overview. For
23full details, you should refer to the documentation for Python 2.4.
24% add hyperlink when the documentation becomes available online.
25If you want to understand the complete implementation and design
26rationale, refer to the PEP for a particular new feature.
27
28
29%======================================================================
Andrew M. Kuchling1a420252003-11-08 15:58:49 +000030\section{PEP 322: Reverse Iteration}
Fred Drakeed0fa3d2003-07-30 19:14:09 +000031
Andrew M. Kuchling1a420252003-11-08 15:58:49 +000032A new built-in function, \function{reversed(seq)}, takes a sequence
33and returns an iterator that returns the elements of the sequence
34in reverse order.
35
36\begin{verbatim}
Raymond Hettingerbc3cba22003-11-12 16:39:30 +000037>>> for i in reversed(xrange(1,4)):
Andrew M. Kuchling1a420252003-11-08 15:58:49 +000038... print i
39...
403
412
421
43\end{verbatim}
44
Raymond Hettingerbc3cba22003-11-12 16:39:30 +000045Compared to extended slicing, \code{range(1,4)[::-1]}, \function{reversed()}
46is easier to read, runs faster, and uses substantially less memory.
47
Andrew M. Kuchling1a420252003-11-08 15:58:49 +000048Note that \function{reversed()} only accepts sequences, not arbitrary
Raymond Hettingerbc3cba22003-11-12 16:39:30 +000049iterators. If you want to reverse an iterator, first convert it to
50a list with \function{list()}.
Andrew M. Kuchling1a420252003-11-08 15:58:49 +000051
52\begin{verbatim}
53>>> input = open('/etc/passwd', 'r')
54>>> for line in reversed(list(input)):
55... print line
56...
57root:*:0:0:System Administrator:/var/root:/bin/tcsh
58 ...
59\end{verbatim}
Fred Drakeed0fa3d2003-07-30 19:14:09 +000060
Andrew M. Kuchlingf7a6b672003-11-08 16:05:37 +000061\begin{seealso}
62\seepep{322}{Reverse Iteration}{Written and implemented by Raymond Hettinger.}
63
64\end{seealso}
65
Fred Drakeed0fa3d2003-07-30 19:14:09 +000066
67%======================================================================
68\section{Other Language Changes}
69
70Here are all of the changes that Python 2.4 makes to the core Python
71language.
72
73\begin{itemize}
Andrew M. Kuchling2fb4d512003-10-21 12:31:16 +000074\item The \method{sort()} method of lists gained three keyword
75arguments, \var{cmp}, \var{key}, and \var{reverse}. These arguments
76make some common usages of \method{sort()} simpler. All are optional.
77
78\var{cmp} is the same as the previous single argument to
79\method{sort()}; if provided, the value should be a comparison
80function that takes two arguments and returns -1, 0, or +1 depending
81on how the arguments compare.
82
83\var{key} should be a single-argument function that takes a list
84element and returns a comparison key for the element. The list is
Raymond Hettinger607c00f2003-11-12 16:27:50 +000085then sorted using the comparison keys. The following example sorts a
86list case-insensitively:
Andrew M. Kuchling2fb4d512003-10-21 12:31:16 +000087
88\begin{verbatim}
89>>> L = ['A', 'b', 'c', 'D']
90>>> L.sort() # Case-sensitive sort
91>>> L
92['A', 'D', 'b', 'c']
93>>> L.sort(key=lambda x: x.lower())
94>>> L
95['A', 'b', 'c', 'D']
96>>> L.sort(cmp=lambda x,y: cmp(x.lower(), y.lower()))
97>>> L
98['A', 'b', 'c', 'D']
99\end{verbatim}
100
101The last example, which uses the \var{cmp} parameter, is the old way
102to perform a case-insensitive sort. It works, but is slower than
103using a \var{key} parameter. Using \var{key} results in calling the
104\method{lower()} method once for each element in the list while using
105\var{cmp} will call the method twice for each comparison.
106
Andrew M. Kuchling981a9182003-11-13 21:33:26 +0000107For simple key functions and comparison functions, it is often
108possible to avoid a \keyword{lambda} expression by using an unbound
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000109method instead. For example, the above case-insensitive sort is best
110coded as:
111
112\begin{verbatim}
113>>> L.sort(key=str.lower)
114>>> L
115['A', 'b', 'c', 'D']
116\end{verbatim}
117
Andrew M. Kuchling2fb4d512003-10-21 12:31:16 +0000118The \var{reverse} parameter should have a Boolean value. If the value is
119\constant{True}, the list will be sorted into reverse order. Instead
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000120of \code{L.sort(lambda x,y: cmp(y.score, x.score))}, you can now write:
121\code{L.sort(key = lambda x: x.score, reverse=True)}.
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000122
Andrew M. Kuchling981a9182003-11-13 21:33:26 +0000123The results of sorting are now guaranteed to be stable. This means
124that two entries with equal keys will be returned in the same order as
125they were input. For example, you can sort a list of people by name,
126and then sort the list by age, resulting in a list sorted by age where
127people with the same age are in name-sorted order.
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000128
129\item The list type gained a \method{sorted(iterable)} method that works
130like the in-place \method{sort()} method but has been made suitable for
131use in expressions. The differences are:
132 \begin{itemize}
Raymond Hettinger7d1dd042003-11-12 16:42:10 +0000133 \item the input may be any iterable;
134 \item a newly formed copy is sorted, leaving the original intact; and
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000135 \item the expression returns the new sorted copy
136 \end{itemize}
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000137
138\begin{verbatim}
139>>> L = [9,7,8,3,2,4,1,6,5]
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000140>>> [10+i for i in list.sorted(L)] # usable in a list comprehension
141[11, 12, 13, 14, 15, 16, 17, 18, 19]
142>>> L = [9,7,8,3,2,4,1,6,5] # original is left unchanged
143[9,7,8,3,2,4,1,6,5]
144>>> list.sorted('Monte Python') # any iterable may be an input
145[' ', 'M', 'P', 'e', 'h', 'n', 'n', 'o', 'o', 't', 't', 'y']
146>>> colormap = dict(red=1, blue=2, green=3, black=4, yellow=5)
Andrew M. Kuchling981a9182003-11-13 21:33:26 +0000147>>> # Lists the contents of the dict sorted by key values
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000148>>> for k, v in list.sorted(colormap.iteritems()):
149... print k, v
150...
151black 4
152blue 2
153green 3
154red 1
155yellow 5
156
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000157\end{verbatim}
158
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000159
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000160\item The \function{zip()} built-in function and \function{itertools.izip()}
161 now return an empty list instead of raising a \exception{TypeError}
162 exception if called with no arguments. This makes the functions more
163 suitable for use with variable length argument lists:
164
165\begin{verbatim}
166>>> def transpose(array):
167... return zip(*array)
168...
169>>> transpose([(1,2,3), (4,5,6)])
170[(1, 4), (2, 5), (3, 6)]
171>>> transpose([])
172[]
173\end{verbatim}
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000174
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000175\end{itemize}
176
177
178%======================================================================
179\subsection{Optimizations}
180
181\begin{itemize}
182
183\item Optimizations should be described here.
184
185\end{itemize}
186
187The net result of the 2.4 optimizations is that Python 2.4 runs the
188pystone benchmark around XX\% faster than Python 2.3 and YY\% faster
189than Python 2.2.
190
191
192%======================================================================
193\section{New, Improved, and Deprecated Modules}
194
195As usual, Python's standard library received a number of enhancements and
196bug fixes. Here's a partial list of the most notable changes, sorted
197alphabetically by module name. Consult the
198\file{Misc/NEWS} file in the source tree for a more
199complete list of changes, or look through the CVS logs for all the
200details.
201
202\begin{itemize}
203
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +0000204\item The \module{curses} modules now supports the ncurses extension
205 \function{use_default_colors()}. On platforms where the terminal
206 supports transparency, this makes it possible to use a transparent background.
207 (Contributed by J\"org Lehmann.)
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000208
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000209\item The \module{heapq} module has been converted to C. The resulting
210 ten-fold improvement in speed makes the module suitable for handling
211 high volumes of data.
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000212
Andrew M. Kuchlingdff9dbd2003-11-20 22:22:19 +0000213\item The \module{imaplib} module now supports IMAP's THREAD command.
214(Contributed by Yves Dionne.)
215
216\item A new \function{getsid()} function was added to the
217\module{posix} module that underlies the \module{os} module.
218(Contributed by J. Raynor.)
219
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000220\item The \module{random} module has a new method called \method{getrandbits(N)}
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000221 which returns an N-bit long integer. This method supports the existing
222 \method{randrange()} method, making it possible to efficiently generate
223 arbitrarily large random numbers (suitable for prime number generation in
224 RSA applications).
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000225
226\item The regular expression language accepted by the \module{re} module
227 was extended with simple conditional expressions, written as
228 \code{(?(\var{group})\var{A}|\var{B})}. \var{group} is either a
229 numeric group ID or a group name defined with \code{(?P<group>...)}
230 earlier in the expression. If the specified group matched, the
231 regular expression pattern \var{A} will be tested against the string; if
232 the group didn't match, the pattern \var{B} will be used instead.
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +0000233
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000234\end{itemize}
235
236
237%======================================================================
238% whole new modules get described in \subsections here
239
240
241% ======================================================================
242\section{Build and C API Changes}
243
244Changes to Python's build process and to the C API include:
245
246\begin{itemize}
247
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000248 \item Three new convenience macros were added for common return
249 values from extension functions: \csimplemacro{Py_RETURN_NONE},
250 \csimplemacro{Py_RETURN_TRUE}, and \csimplemacro{Py_RETURN_FALSE}.
251
252 \item A new function, \cfunction{PyTuple_Pack(N, obj1, obj2, ...,
253 objN)}, constructs tuples from a variable length argument list of
254 Python objects.
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000255
256\end{itemize}
257
258
259%======================================================================
260\subsection{Port-Specific Changes}
261
262Platform-specific changes go here.
263
264
265%======================================================================
266\section{Other Changes and Fixes \label{section-other}}
267
268As usual, there were a bunch of other improvements and bugfixes
269scattered throughout the source tree. A search through the CVS change
270logs finds there were XXX patches applied and YYY bugs fixed between
271Python 2.3 and 2.4. Both figures are likely to be underestimates.
272
273Some of the more notable changes are:
274
275\begin{itemize}
276
277\item Details go here.
278
279\end{itemize}
280
281
282%======================================================================
283\section{Porting to Python 2.4}
284
285This section lists previously described changes that may require
286changes to your code:
287
288\begin{itemize}
289
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000290\item The \function{zip()} built-in function and \function{itertools.izip()}
291 now return an empty list instead of raising a \exception{TypeError}
292 exception if called with no arguments.
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000293
294\item \function{dircache.listdir()} now passes exceptions to the caller
295 instead of returning empty lists.
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000296
297\end{itemize}
298
299
300%======================================================================
301\section{Acknowledgements \label{acks}}
302
303The author would like to thank the following people for offering
304suggestions, corrections and assistance with various drafts of this
Andrew M. Kuchling981a9182003-11-13 21:33:26 +0000305article: Raymond Hettinger.
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000306
307\end{document}