blob: 2f80d8960d801a83f5bc0700f428826148ea8a87 [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
58
59%======================================================================
60\section{Other Language Changes}
61
62Here are all of the changes that Python 2.4 makes to the core Python
63language.
64
65\begin{itemize}
Andrew M. Kuchling2fb4d512003-10-21 12:31:16 +000066\item The \method{sort()} method of lists gained three keyword
67arguments, \var{cmp}, \var{key}, and \var{reverse}. These arguments
68make some common usages of \method{sort()} simpler. All are optional.
69
70\var{cmp} is the same as the previous single argument to
71\method{sort()}; if provided, the value should be a comparison
72function that takes two arguments and returns -1, 0, or +1 depending
73on how the arguments compare.
74
75\var{key} should be a single-argument function that takes a list
76element and returns a comparison key for the element. The list is
77then sorted using the comparison keys. The following example sorts a list
78case-insensitively:
79
80\begin{verbatim}
81>>> L = ['A', 'b', 'c', 'D']
82>>> L.sort() # Case-sensitive sort
83>>> L
84['A', 'D', 'b', 'c']
85>>> L.sort(key=lambda x: x.lower())
86>>> L
87['A', 'b', 'c', 'D']
88>>> L.sort(cmp=lambda x,y: cmp(x.lower(), y.lower()))
89>>> L
90['A', 'b', 'c', 'D']
91\end{verbatim}
92
93The last example, which uses the \var{cmp} parameter, is the old way
94to perform a case-insensitive sort. It works, but is slower than
95using a \var{key} parameter. Using \var{key} results in calling the
96\method{lower()} method once for each element in the list while using
97\var{cmp} will call the method twice for each comparison.
98
99The \var{reverse} parameter should have a Boolean value. If the value is
100\constant{True}, the list will be sorted into reverse order. Instead
101of \code{L.sort() ; L.reverse()}, you can now write
102\code{L.sort(reverse=True)}.
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000103
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000104\item The list type gained a \method{sorted(iterable)} method that
105returns the elements of the iterable as a sorted list. It also accepts
106the \var{cmp}, \var{key}, and \var{reverse} keyword arguments, same as
107the \method{sort()} method. An example usage:
108
109\begin{verbatim}
110>>> L = [9,7,8,3,2,4,1,6,5]
111>>> list.sorted(L)
112[1, 2, 3, 4, 5, 6, 7, 8, 9]
113>>> L
114[9, 7, 8, 3, 2, 4, 1, 6, 5]
115>>>
116\end{verbatim}
117
118Note that the original list is unchanged; the list returned by
119\method{sorted()} is a newly-created one.
120
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000121\item The \function{zip()} built-in function and \function{itertools.izip()} now return an empty list
122 instead of raising a \exception{TypeError} exception if called
123 with no arguments.
124
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000125\end{itemize}
126
127
128%======================================================================
129\subsection{Optimizations}
130
131\begin{itemize}
132
133\item Optimizations should be described here.
134
135\end{itemize}
136
137The net result of the 2.4 optimizations is that Python 2.4 runs the
138pystone benchmark around XX\% faster than Python 2.3 and YY\% faster
139than Python 2.2.
140
141
142%======================================================================
143\section{New, Improved, and Deprecated Modules}
144
145As usual, Python's standard library received a number of enhancements and
146bug fixes. Here's a partial list of the most notable changes, sorted
147alphabetically by module name. Consult the
148\file{Misc/NEWS} file in the source tree for a more
149complete list of changes, or look through the CVS logs for all the
150details.
151
152\begin{itemize}
153
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +0000154\item The \module{curses} modules now supports the ncurses extension
155 \function{use_default_colors()}. On platforms where the terminal
156 supports transparency, this makes it possible to use a transparent background.
157 (Contributed by J\"org Lehmann.)
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000158
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000159\item The \module{heapq} module is no longer implemented in Python,
160 having been converted into C.
161
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000162\item The \module{random} module has a new method called \method{getrandbits(N)}
163 which returns an N-bit long integer.
164
165\item The regular expression language accepted by the \module{re} module
166 was extended with simple conditional expressions, written as
167 \code{(?(\var{group})\var{A}|\var{B})}. \var{group} is either a
168 numeric group ID or a group name defined with \code{(?P<group>...)}
169 earlier in the expression. If the specified group matched, the
170 regular expression pattern \var{A} will be tested against the string; if
171 the group didn't match, the pattern \var{B} will be used instead.
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +0000172
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000173\end{itemize}
174
175
176%======================================================================
177% whole new modules get described in \subsections here
178
179
180% ======================================================================
181\section{Build and C API Changes}
182
183Changes to Python's build process and to the C API include:
184
185\begin{itemize}
186
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000187 \item Three new convenience macros were added for common return
188 values from extension functions: \csimplemacro{Py_RETURN_NONE},
189 \csimplemacro{Py_RETURN_TRUE}, and \csimplemacro{Py_RETURN_FALSE}.
190
191 \item A new function, \cfunction{PyTuple_Pack(N, obj1, obj2, ...,
192 objN)}, constructs tuples from a variable length argument list of
193 Python objects.
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000194
195\end{itemize}
196
197
198%======================================================================
199\subsection{Port-Specific Changes}
200
201Platform-specific changes go here.
202
203
204%======================================================================
205\section{Other Changes and Fixes \label{section-other}}
206
207As usual, there were a bunch of other improvements and bugfixes
208scattered throughout the source tree. A search through the CVS change
209logs finds there were XXX patches applied and YYY bugs fixed between
210Python 2.3 and 2.4. Both figures are likely to be underestimates.
211
212Some of the more notable changes are:
213
214\begin{itemize}
215
216\item Details go here.
217
218\end{itemize}
219
220
221%======================================================================
222\section{Porting to Python 2.4}
223
224This section lists previously described changes that may require
225changes to your code:
226
227\begin{itemize}
228
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000229\item The \function{zip()} built-in function and \function{itertools.izip()} now return an empty list
230 instead of raising a \exception{TypeError} exception if called
231 with no arguments.
232
233\item \function{dircache.listdir()} now passes exceptions to the caller
234 instead of returning empty lists.
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000235
236\end{itemize}
237
238
239%======================================================================
240\section{Acknowledgements \label{acks}}
241
242The author would like to thank the following people for offering
243suggestions, corrections and assistance with various drafts of this
244article: .
245
246\end{document}