blob: 68742a8792a9c956aafdefbeb792d214d4aabe6d [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}
37>>> for i in reversed([1,2,3]):
38... print i
39...
403
412
421
43\end{verbatim}
44
45Note that \function{reversed()} only accepts sequences, not arbitrary
46iterators. If you want to reverse an iterator, convert it to
47a list or tuple with \function{list()} or \function{tuple()}.
48
49\begin{verbatim}
50>>> input = open('/etc/passwd', 'r')
51>>> for line in reversed(list(input)):
52... print line
53...
54root:*:0:0:System Administrator:/var/root:/bin/tcsh
55 ...
56\end{verbatim}
Fred Drakeed0fa3d2003-07-30 19:14:09 +000057
Andrew M. Kuchlingf7a6b672003-11-08 16:05:37 +000058\begin{seealso}
59\seepep{322}{Reverse Iteration}{Written and implemented by Raymond Hettinger.}
60
61\end{seealso}
62
Fred Drakeed0fa3d2003-07-30 19:14:09 +000063
64%======================================================================
65\section{Other Language Changes}
66
67Here are all of the changes that Python 2.4 makes to the core Python
68language.
69
70\begin{itemize}
Andrew M. Kuchling2fb4d512003-10-21 12:31:16 +000071\item The \method{sort()} method of lists gained three keyword
72arguments, \var{cmp}, \var{key}, and \var{reverse}. These arguments
73make some common usages of \method{sort()} simpler. All are optional.
74
75\var{cmp} is the same as the previous single argument to
76\method{sort()}; if provided, the value should be a comparison
77function that takes two arguments and returns -1, 0, or +1 depending
78on how the arguments compare.
79
80\var{key} should be a single-argument function that takes a list
81element and returns a comparison key for the element. The list is
Raymond Hettinger607c00f2003-11-12 16:27:50 +000082then sorted using the comparison keys. The following example sorts a
83list case-insensitively:
Andrew M. Kuchling2fb4d512003-10-21 12:31:16 +000084
85\begin{verbatim}
86>>> L = ['A', 'b', 'c', 'D']
87>>> L.sort() # Case-sensitive sort
88>>> L
89['A', 'D', 'b', 'c']
90>>> L.sort(key=lambda x: x.lower())
91>>> L
92['A', 'b', 'c', 'D']
93>>> L.sort(cmp=lambda x,y: cmp(x.lower(), y.lower()))
94>>> L
95['A', 'b', 'c', 'D']
96\end{verbatim}
97
98The last example, which uses the \var{cmp} parameter, is the old way
99to perform a case-insensitive sort. It works, but is slower than
100using a \var{key} parameter. Using \var{key} results in calling the
101\method{lower()} method once for each element in the list while using
102\var{cmp} will call the method twice for each comparison.
103
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000104Note, for simple key functions and comparison functions, it is often
105possible to avoid the \keyword{lambda} expression by using an unbound
106method instead. For example, the above case-insensitive sort is best
107coded as:
108
109\begin{verbatim}
110>>> L.sort(key=str.lower)
111>>> L
112['A', 'b', 'c', 'D']
113\end{verbatim}
114
Andrew M. Kuchling2fb4d512003-10-21 12:31:16 +0000115The \var{reverse} parameter should have a Boolean value. If the value is
116\constant{True}, the list will be sorted into reverse order. Instead
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000117of \code{L.sort(lambda x,y: cmp(y.score, x.score))}, you can now write:
118\code{L.sort(key = lambda x: x.score, reverse=True)}.
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000119
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000120The results of sorting are now guaranteed to be stable. This means that
121two entries with equal keys will be returned in the same order as
122they were input.
123
124
125\item The list type gained a \method{sorted(iterable)} method that works
126like the in-place \method{sort()} method but has been made suitable for
127use in expressions. The differences are:
128 \begin{itemize}
129 \item the input make be any iterable;
130 \item a copy is sorted, leaving the original intact; and
131 \item the expression returns the new sorted copy
132 \end{itemize}
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000133
134\begin{verbatim}
135>>> L = [9,7,8,3,2,4,1,6,5]
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000136>>> [10+i for i in list.sorted(L)] # usable in a list comprehension
137[11, 12, 13, 14, 15, 16, 17, 18, 19]
138>>> L = [9,7,8,3,2,4,1,6,5] # original is left unchanged
139[9,7,8,3,2,4,1,6,5]
140>>> list.sorted('Monte Python') # any iterable may be an input
141[' ', 'M', 'P', 'e', 'h', 'n', 'n', 'o', 'o', 't', 't', 'y']
142>>> colormap = dict(red=1, blue=2, green=3, black=4, yellow=5)
143>>> for k, v in list.sorted(colormap.iteritems()):
144... print k, v
145...
146black 4
147blue 2
148green 3
149red 1
150yellow 5
151
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000152\end{verbatim}
153
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000154
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000155\item The \function{zip()} built-in function and \function{itertools.izip()}
156 now return an empty list instead of raising a \exception{TypeError}
157 exception if called with no arguments. This makes the functions more
158 suitable for use with variable length argument lists:
159
160\begin{verbatim}
161>>> def transpose(array):
162... return zip(*array)
163...
164>>> transpose([(1,2,3), (4,5,6)])
165[(1, 4), (2, 5), (3, 6)]
166>>> transpose([])
167[]
168\end{verbatim}
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000169
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000170\end{itemize}
171
172
173%======================================================================
174\subsection{Optimizations}
175
176\begin{itemize}
177
178\item Optimizations should be described here.
179
180\end{itemize}
181
182The net result of the 2.4 optimizations is that Python 2.4 runs the
183pystone benchmark around XX\% faster than Python 2.3 and YY\% faster
184than Python 2.2.
185
186
187%======================================================================
188\section{New, Improved, and Deprecated Modules}
189
190As usual, Python's standard library received a number of enhancements and
191bug fixes. Here's a partial list of the most notable changes, sorted
192alphabetically by module name. Consult the
193\file{Misc/NEWS} file in the source tree for a more
194complete list of changes, or look through the CVS logs for all the
195details.
196
197\begin{itemize}
198
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +0000199\item The \module{curses} modules now supports the ncurses extension
200 \function{use_default_colors()}. On platforms where the terminal
201 supports transparency, this makes it possible to use a transparent background.
202 (Contributed by J\"org Lehmann.)
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000203
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000204\item The \module{heapq} module has been converted to C. The resulting
205 ten-fold improvement in speed makes the module suitable for handling
206 high volumes of data.
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000207
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000208\item The \module{random} module has a new method called \method{getrandbits(N)}
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000209 which returns an N-bit long integer. This method supports the existing
210 \method{randrange()} method, making it possible to efficiently generate
211 arbitrarily large random numbers (suitable for prime number generation in
212 RSA applications).
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000213
214\item The regular expression language accepted by the \module{re} module
215 was extended with simple conditional expressions, written as
216 \code{(?(\var{group})\var{A}|\var{B})}. \var{group} is either a
217 numeric group ID or a group name defined with \code{(?P<group>...)}
218 earlier in the expression. If the specified group matched, the
219 regular expression pattern \var{A} will be tested against the string; if
220 the group didn't match, the pattern \var{B} will be used instead.
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +0000221
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000222\end{itemize}
223
224
225%======================================================================
226% whole new modules get described in \subsections here
227
228
229% ======================================================================
230\section{Build and C API Changes}
231
232Changes to Python's build process and to the C API include:
233
234\begin{itemize}
235
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000236 \item Three new convenience macros were added for common return
237 values from extension functions: \csimplemacro{Py_RETURN_NONE},
238 \csimplemacro{Py_RETURN_TRUE}, and \csimplemacro{Py_RETURN_FALSE}.
239
240 \item A new function, \cfunction{PyTuple_Pack(N, obj1, obj2, ...,
241 objN)}, constructs tuples from a variable length argument list of
242 Python objects.
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000243
244\end{itemize}
245
246
247%======================================================================
248\subsection{Port-Specific Changes}
249
250Platform-specific changes go here.
251
252
253%======================================================================
254\section{Other Changes and Fixes \label{section-other}}
255
256As usual, there were a bunch of other improvements and bugfixes
257scattered throughout the source tree. A search through the CVS change
258logs finds there were XXX patches applied and YYY bugs fixed between
259Python 2.3 and 2.4. Both figures are likely to be underestimates.
260
261Some of the more notable changes are:
262
263\begin{itemize}
264
265\item Details go here.
266
267\end{itemize}
268
269
270%======================================================================
271\section{Porting to Python 2.4}
272
273This section lists previously described changes that may require
274changes to your code:
275
276\begin{itemize}
277
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000278\item The \function{zip()} built-in function and \function{itertools.izip()}
279 now return an empty list instead of raising a \exception{TypeError}
280 exception if called with no arguments.
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000281
282\item \function{dircache.listdir()} now passes exceptions to the caller
283 instead of returning empty lists.
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000284
285\end{itemize}
286
287
288%======================================================================
289\section{Acknowledgements \label{acks}}
290
291The author would like to thank the following people for offering
292suggestions, corrections and assistance with various drafts of this
293article: .
294
295\end{document}