blob: 0f5e546db909be8e98420dd265c3fd008196480d [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
Raymond Hettinger7e0282f2003-11-24 07:14:54 +000028%======================================================================
29\section{PEP 218: Built-In Set Objects}
30
31Two new built-in types, \function{set(iterable)} and
32\function{frozenset(iterable)} provide high speed data types for
33membership testing, for eliminating duplicates from sequences, and
34for mathematical operations like unions, intersections, differences,
35and symmetric differences.
36
37\begin{verbatim}
38>>> a = set('abracadabra') # form a set from a string
39>>> 'z' in a # fast membership testing
40False
41>>> a # unique letters in a
42set(['a', 'r', 'b', 'c', 'd'])
43>>> ''.join(a) # convert back into a string
44'arbcd'
45>>> b = set('alacazam') # form a second set
46>>> a - b # letters in a but not in b
47set(['r', 'd', 'b'])
48>>> a | b # letters in either a or b
49set(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'])
50>>> a & b # letters in both a and b
51set(['a', 'c'])
52>>> a ^ b # letters in a or b but not both
53set(['r', 'd', 'b', 'm', 'z', 'l'])
54>>> a.add('z') # add a new element
55>>> a.update('wxy') # add multiple new elements
56>>> a
57set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'x', 'z'])
58>>> a.remove('x') # take one element out
59>>> a
60set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'z'])
61\end{verbatim}
62
63The type \function{frozenset()} is an immutable version of \function{set()}.
64Since it is immutable and hashable, it may be used as a dictionary key or
65as a member of another set. Accordingly, it does not have methods
66like \method{add()} and \method{remove()} which could alter its contents.
67
68\begin{seealso}
69\seepep{218}{Adding a Built-In Set Object Type}{Originally proposed by
70Greg Wilson and ultimately implemented by Raymond Hettinger.}
71\end{seealso}
Fred Drakeed0fa3d2003-07-30 19:14:09 +000072
73%======================================================================
Andrew M. Kuchling1a420252003-11-08 15:58:49 +000074\section{PEP 322: Reverse Iteration}
Fred Drakeed0fa3d2003-07-30 19:14:09 +000075
Andrew M. Kuchling1a420252003-11-08 15:58:49 +000076A new built-in function, \function{reversed(seq)}, takes a sequence
77and returns an iterator that returns the elements of the sequence
78in reverse order.
79
80\begin{verbatim}
Raymond Hettingerbc3cba22003-11-12 16:39:30 +000081>>> for i in reversed(xrange(1,4)):
Andrew M. Kuchling1a420252003-11-08 15:58:49 +000082... print i
83...
843
852
861
87\end{verbatim}
88
Raymond Hettingerbc3cba22003-11-12 16:39:30 +000089Compared to extended slicing, \code{range(1,4)[::-1]}, \function{reversed()}
90is easier to read, runs faster, and uses substantially less memory.
91
Andrew M. Kuchling1a420252003-11-08 15:58:49 +000092Note that \function{reversed()} only accepts sequences, not arbitrary
Raymond Hettingerbc3cba22003-11-12 16:39:30 +000093iterators. If you want to reverse an iterator, first convert it to
94a list with \function{list()}.
Andrew M. Kuchling1a420252003-11-08 15:58:49 +000095
96\begin{verbatim}
97>>> input = open('/etc/passwd', 'r')
98>>> for line in reversed(list(input)):
99... print line
100...
101root:*:0:0:System Administrator:/var/root:/bin/tcsh
102 ...
103\end{verbatim}
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000104
Andrew M. Kuchlingf7a6b672003-11-08 16:05:37 +0000105\begin{seealso}
106\seepep{322}{Reverse Iteration}{Written and implemented by Raymond Hettinger.}
107
108\end{seealso}
109
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000110
111%======================================================================
112\section{Other Language Changes}
113
114Here are all of the changes that Python 2.4 makes to the core Python
115language.
116
117\begin{itemize}
Andrew M. Kuchling2fb4d512003-10-21 12:31:16 +0000118\item The \method{sort()} method of lists gained three keyword
119arguments, \var{cmp}, \var{key}, and \var{reverse}. These arguments
120make some common usages of \method{sort()} simpler. All are optional.
121
122\var{cmp} is the same as the previous single argument to
123\method{sort()}; if provided, the value should be a comparison
124function that takes two arguments and returns -1, 0, or +1 depending
125on how the arguments compare.
126
127\var{key} should be a single-argument function that takes a list
128element and returns a comparison key for the element. The list is
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000129then sorted using the comparison keys. The following example sorts a
130list case-insensitively:
Andrew M. Kuchling2fb4d512003-10-21 12:31:16 +0000131
132\begin{verbatim}
133>>> L = ['A', 'b', 'c', 'D']
134>>> L.sort() # Case-sensitive sort
135>>> L
136['A', 'D', 'b', 'c']
137>>> L.sort(key=lambda x: x.lower())
138>>> L
139['A', 'b', 'c', 'D']
140>>> L.sort(cmp=lambda x,y: cmp(x.lower(), y.lower()))
141>>> L
142['A', 'b', 'c', 'D']
143\end{verbatim}
144
145The last example, which uses the \var{cmp} parameter, is the old way
146to perform a case-insensitive sort. It works, but is slower than
147using a \var{key} parameter. Using \var{key} results in calling the
148\method{lower()} method once for each element in the list while using
149\var{cmp} will call the method twice for each comparison.
150
Andrew M. Kuchling981a9182003-11-13 21:33:26 +0000151For simple key functions and comparison functions, it is often
152possible to avoid a \keyword{lambda} expression by using an unbound
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000153method instead. For example, the above case-insensitive sort is best
154coded as:
155
156\begin{verbatim}
157>>> L.sort(key=str.lower)
158>>> L
159['A', 'b', 'c', 'D']
160\end{verbatim}
161
Andrew M. Kuchling2fb4d512003-10-21 12:31:16 +0000162The \var{reverse} parameter should have a Boolean value. If the value is
163\constant{True}, the list will be sorted into reverse order. Instead
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000164of \code{L.sort(lambda x,y: cmp(y.score, x.score))}, you can now write:
165\code{L.sort(key = lambda x: x.score, reverse=True)}.
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000166
Andrew M. Kuchling981a9182003-11-13 21:33:26 +0000167The results of sorting are now guaranteed to be stable. This means
168that two entries with equal keys will be returned in the same order as
169they were input. For example, you can sort a list of people by name,
170and then sort the list by age, resulting in a list sorted by age where
171people with the same age are in name-sorted order.
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000172
173\item The list type gained a \method{sorted(iterable)} method that works
174like the in-place \method{sort()} method but has been made suitable for
175use in expressions. The differences are:
176 \begin{itemize}
Raymond Hettinger7d1dd042003-11-12 16:42:10 +0000177 \item the input may be any iterable;
178 \item a newly formed copy is sorted, leaving the original intact; and
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000179 \item the expression returns the new sorted copy
180 \end{itemize}
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000181
182\begin{verbatim}
183>>> L = [9,7,8,3,2,4,1,6,5]
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000184>>> [10+i for i in list.sorted(L)] # usable in a list comprehension
185[11, 12, 13, 14, 15, 16, 17, 18, 19]
186>>> L = [9,7,8,3,2,4,1,6,5] # original is left unchanged
187[9,7,8,3,2,4,1,6,5]
188>>> list.sorted('Monte Python') # any iterable may be an input
189[' ', 'M', 'P', 'e', 'h', 'n', 'n', 'o', 'o', 't', 't', 'y']
190>>> colormap = dict(red=1, blue=2, green=3, black=4, yellow=5)
Andrew M. Kuchling981a9182003-11-13 21:33:26 +0000191>>> # Lists the contents of the dict sorted by key values
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000192>>> for k, v in list.sorted(colormap.iteritems()):
193... print k, v
194...
195black 4
196blue 2
197green 3
198red 1
199yellow 5
200
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000201\end{verbatim}
202
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000203
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000204\item The \function{zip()} built-in function and \function{itertools.izip()}
205 now return an empty list instead of raising a \exception{TypeError}
206 exception if called with no arguments. This makes the functions more
207 suitable for use with variable length argument lists:
208
209\begin{verbatim}
210>>> def transpose(array):
211... return zip(*array)
212...
213>>> transpose([(1,2,3), (4,5,6)])
214[(1, 4), (2, 5), (3, 6)]
215>>> transpose([])
216[]
217\end{verbatim}
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000218
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000219\end{itemize}
220
221
222%======================================================================
223\subsection{Optimizations}
224
225\begin{itemize}
226
227\item Optimizations should be described here.
228
229\end{itemize}
230
231The net result of the 2.4 optimizations is that Python 2.4 runs the
232pystone benchmark around XX\% faster than Python 2.3 and YY\% faster
233than Python 2.2.
234
235
236%======================================================================
237\section{New, Improved, and Deprecated Modules}
238
239As usual, Python's standard library received a number of enhancements and
240bug fixes. Here's a partial list of the most notable changes, sorted
241alphabetically by module name. Consult the
242\file{Misc/NEWS} file in the source tree for a more
243complete list of changes, or look through the CVS logs for all the
244details.
245
246\begin{itemize}
247
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +0000248\item The \module{curses} modules now supports the ncurses extension
249 \function{use_default_colors()}. On platforms where the terminal
250 supports transparency, this makes it possible to use a transparent background.
251 (Contributed by J\"org Lehmann.)
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000252
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000253\item The \module{heapq} module has been converted to C. The resulting
254 ten-fold improvement in speed makes the module suitable for handling
255 high volumes of data.
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000256
Andrew M. Kuchlingdff9dbd2003-11-20 22:22:19 +0000257\item The \module{imaplib} module now supports IMAP's THREAD command.
258(Contributed by Yves Dionne.)
259
260\item A new \function{getsid()} function was added to the
261\module{posix} module that underlies the \module{os} module.
262(Contributed by J. Raynor.)
263
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000264\item The \module{random} module has a new method called \method{getrandbits(N)}
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000265 which returns an N-bit long integer. This method supports the existing
266 \method{randrange()} method, making it possible to efficiently generate
267 arbitrarily large random numbers (suitable for prime number generation in
268 RSA applications).
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000269
270\item The regular expression language accepted by the \module{re} module
271 was extended with simple conditional expressions, written as
272 \code{(?(\var{group})\var{A}|\var{B})}. \var{group} is either a
273 numeric group ID or a group name defined with \code{(?P<group>...)}
274 earlier in the expression. If the specified group matched, the
275 regular expression pattern \var{A} will be tested against the string; if
276 the group didn't match, the pattern \var{B} will be used instead.
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +0000277
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000278\end{itemize}
279
280
281%======================================================================
282% whole new modules get described in \subsections here
283
284
285% ======================================================================
286\section{Build and C API Changes}
287
288Changes to Python's build process and to the C API include:
289
290\begin{itemize}
291
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000292 \item Three new convenience macros were added for common return
293 values from extension functions: \csimplemacro{Py_RETURN_NONE},
294 \csimplemacro{Py_RETURN_TRUE}, and \csimplemacro{Py_RETURN_FALSE}.
295
296 \item A new function, \cfunction{PyTuple_Pack(N, obj1, obj2, ...,
297 objN)}, constructs tuples from a variable length argument list of
298 Python objects.
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000299
300\end{itemize}
301
302
303%======================================================================
304\subsection{Port-Specific Changes}
305
306Platform-specific changes go here.
307
308
309%======================================================================
310\section{Other Changes and Fixes \label{section-other}}
311
312As usual, there were a bunch of other improvements and bugfixes
313scattered throughout the source tree. A search through the CVS change
314logs finds there were XXX patches applied and YYY bugs fixed between
315Python 2.3 and 2.4. Both figures are likely to be underestimates.
316
317Some of the more notable changes are:
318
319\begin{itemize}
320
321\item Details go here.
322
323\end{itemize}
324
325
326%======================================================================
327\section{Porting to Python 2.4}
328
329This section lists previously described changes that may require
330changes to your code:
331
332\begin{itemize}
333
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000334\item The \function{zip()} built-in function and \function{itertools.izip()}
335 now return an empty list instead of raising a \exception{TypeError}
336 exception if called with no arguments.
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000337
338\item \function{dircache.listdir()} now passes exceptions to the caller
339 instead of returning empty lists.
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000340
341\end{itemize}
342
343
344%======================================================================
345\section{Acknowledgements \label{acks}}
346
347The author would like to thank the following people for offering
348suggestions, corrections and assistance with various drafts of this
Andrew M. Kuchling981a9182003-11-13 21:33:26 +0000349article: Raymond Hettinger.
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000350
351\end{document}