blob: 402fb3eacab49fada66d86c18075a13706133654 [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
82then sorted using the comparison keys. The following example sorts a list
83case-insensitively:
84
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
104The \var{reverse} parameter should have a Boolean value. If the value is
105\constant{True}, the list will be sorted into reverse order. Instead
106of \code{L.sort() ; L.reverse()}, you can now write
107\code{L.sort(reverse=True)}.
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000108
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000109\item The list type gained a \method{sorted(iterable)} method that
110returns the elements of the iterable as a sorted list. It also accepts
111the \var{cmp}, \var{key}, and \var{reverse} keyword arguments, same as
112the \method{sort()} method. An example usage:
113
114\begin{verbatim}
115>>> L = [9,7,8,3,2,4,1,6,5]
116>>> list.sorted(L)
117[1, 2, 3, 4, 5, 6, 7, 8, 9]
118>>> L
119[9, 7, 8, 3, 2, 4, 1, 6, 5]
120>>>
121\end{verbatim}
122
123Note that the original list is unchanged; the list returned by
124\method{sorted()} is a newly-created one.
125
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000126\item The \function{zip()} built-in function and \function{itertools.izip()} now return an empty list
127 instead of raising a \exception{TypeError} exception if called
128 with no arguments.
129
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000130\end{itemize}
131
132
133%======================================================================
134\subsection{Optimizations}
135
136\begin{itemize}
137
138\item Optimizations should be described here.
139
140\end{itemize}
141
142The net result of the 2.4 optimizations is that Python 2.4 runs the
143pystone benchmark around XX\% faster than Python 2.3 and YY\% faster
144than Python 2.2.
145
146
147%======================================================================
148\section{New, Improved, and Deprecated Modules}
149
150As usual, Python's standard library received a number of enhancements and
151bug fixes. Here's a partial list of the most notable changes, sorted
152alphabetically by module name. Consult the
153\file{Misc/NEWS} file in the source tree for a more
154complete list of changes, or look through the CVS logs for all the
155details.
156
157\begin{itemize}
158
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +0000159\item The \module{curses} modules now supports the ncurses extension
160 \function{use_default_colors()}. On platforms where the terminal
161 supports transparency, this makes it possible to use a transparent background.
162 (Contributed by J\"org Lehmann.)
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000163
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000164\item The \module{heapq} module is no longer implemented in Python,
165 having been converted into C.
166
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000167\item The \module{random} module has a new method called \method{getrandbits(N)}
168 which returns an N-bit long integer.
169
170\item The regular expression language accepted by the \module{re} module
171 was extended with simple conditional expressions, written as
172 \code{(?(\var{group})\var{A}|\var{B})}. \var{group} is either a
173 numeric group ID or a group name defined with \code{(?P<group>...)}
174 earlier in the expression. If the specified group matched, the
175 regular expression pattern \var{A} will be tested against the string; if
176 the group didn't match, the pattern \var{B} will be used instead.
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +0000177
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000178\end{itemize}
179
180
181%======================================================================
182% whole new modules get described in \subsections here
183
184
185% ======================================================================
186\section{Build and C API Changes}
187
188Changes to Python's build process and to the C API include:
189
190\begin{itemize}
191
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000192 \item Three new convenience macros were added for common return
193 values from extension functions: \csimplemacro{Py_RETURN_NONE},
194 \csimplemacro{Py_RETURN_TRUE}, and \csimplemacro{Py_RETURN_FALSE}.
195
196 \item A new function, \cfunction{PyTuple_Pack(N, obj1, obj2, ...,
197 objN)}, constructs tuples from a variable length argument list of
198 Python objects.
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000199
200\end{itemize}
201
202
203%======================================================================
204\subsection{Port-Specific Changes}
205
206Platform-specific changes go here.
207
208
209%======================================================================
210\section{Other Changes and Fixes \label{section-other}}
211
212As usual, there were a bunch of other improvements and bugfixes
213scattered throughout the source tree. A search through the CVS change
214logs finds there were XXX patches applied and YYY bugs fixed between
215Python 2.3 and 2.4. Both figures are likely to be underestimates.
216
217Some of the more notable changes are:
218
219\begin{itemize}
220
221\item Details go here.
222
223\end{itemize}
224
225
226%======================================================================
227\section{Porting to Python 2.4}
228
229This section lists previously described changes that may require
230changes to your code:
231
232\begin{itemize}
233
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000234\item The \function{zip()} built-in function and \function{itertools.izip()} now return an empty list
235 instead of raising a \exception{TypeError} exception if called
236 with no arguments.
237
238\item \function{dircache.listdir()} now passes exceptions to the caller
239 instead of returning empty lists.
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000240
241\end{itemize}
242
243
244%======================================================================
245\section{Acknowledgements \label{acks}}
246
247The author would like to thank the following people for offering
248suggestions, corrections and assistance with various drafts of this
249article: .
250
251\end{document}