Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 1 | \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 | |
| 14 | This article explains the new features in Python 2.4. No release date |
| 15 | for Python 2.4 has been set; expect that this will happen in 2004. |
| 16 | |
| 17 | While Python 2.3 was primarily a library development release, Python |
| 18 | 2.4 may extend the core language and interpreter in |
| 19 | as-yet-undetermined ways. |
| 20 | |
| 21 | This article doesn't attempt to provide a complete specification of |
| 22 | the new features, but instead provides a convenient overview. For |
| 23 | full details, you should refer to the documentation for Python 2.4. |
| 24 | % add hyperlink when the documentation becomes available online. |
| 25 | If you want to understand the complete implementation and design |
| 26 | rationale, refer to the PEP for a particular new feature. |
| 27 | |
| 28 | |
| 29 | %====================================================================== |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 30 | \section{PEP 322: Reverse Iteration} |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 31 | |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 32 | A new built-in function, \function{reversed(seq)}, takes a sequence |
| 33 | and returns an iterator that returns the elements of the sequence |
| 34 | in reverse order. |
| 35 | |
| 36 | \begin{verbatim} |
| 37 | >>> for i in reversed([1,2,3]): |
| 38 | ... print i |
| 39 | ... |
| 40 | 3 |
| 41 | 2 |
| 42 | 1 |
| 43 | \end{verbatim} |
| 44 | |
| 45 | Note that \function{reversed()} only accepts sequences, not arbitrary |
| 46 | iterators. If you want to reverse an iterator, convert it to |
| 47 | a 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 | ... |
| 54 | root:*:0:0:System Administrator:/var/root:/bin/tcsh |
| 55 | ... |
| 56 | \end{verbatim} |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 57 | |
Andrew M. Kuchling | f7a6b67 | 2003-11-08 16:05:37 +0000 | [diff] [blame] | 58 | \begin{seealso} |
| 59 | \seepep{322}{Reverse Iteration}{Written and implemented by Raymond Hettinger.} |
| 60 | |
| 61 | \end{seealso} |
| 62 | |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 63 | |
| 64 | %====================================================================== |
| 65 | \section{Other Language Changes} |
| 66 | |
| 67 | Here are all of the changes that Python 2.4 makes to the core Python |
| 68 | language. |
| 69 | |
| 70 | \begin{itemize} |
Andrew M. Kuchling | 2fb4d51 | 2003-10-21 12:31:16 +0000 | [diff] [blame] | 71 | \item The \method{sort()} method of lists gained three keyword |
| 72 | arguments, \var{cmp}, \var{key}, and \var{reverse}. These arguments |
| 73 | make 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 |
| 77 | function that takes two arguments and returns -1, 0, or +1 depending |
| 78 | on how the arguments compare. |
| 79 | |
| 80 | \var{key} should be a single-argument function that takes a list |
| 81 | element and returns a comparison key for the element. The list is |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame^] | 82 | then sorted using the comparison keys. The following example sorts a |
| 83 | list case-insensitively: |
Andrew M. Kuchling | 2fb4d51 | 2003-10-21 12:31:16 +0000 | [diff] [blame] | 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 | |
| 98 | The last example, which uses the \var{cmp} parameter, is the old way |
| 99 | to perform a case-insensitive sort. It works, but is slower than |
| 100 | using 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 Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame^] | 104 | Note, for simple key functions and comparison functions, it is often |
| 105 | possible to avoid the \keyword{lambda} expression by using an unbound |
| 106 | method instead. For example, the above case-insensitive sort is best |
| 107 | coded as: |
| 108 | |
| 109 | \begin{verbatim} |
| 110 | >>> L.sort(key=str.lower) |
| 111 | >>> L |
| 112 | ['A', 'b', 'c', 'D'] |
| 113 | \end{verbatim} |
| 114 | |
Andrew M. Kuchling | 2fb4d51 | 2003-10-21 12:31:16 +0000 | [diff] [blame] | 115 | The \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 Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame^] | 117 | of \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 Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 119 | |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame^] | 120 | The results of sorting are now guaranteed to be stable. This means that |
| 121 | two entries with equal keys will be returned in the same order as |
| 122 | they were input. |
| 123 | |
| 124 | |
| 125 | \item The list type gained a \method{sorted(iterable)} method that works |
| 126 | like the in-place \method{sort()} method but has been made suitable for |
| 127 | use 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. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 133 | |
| 134 | \begin{verbatim} |
| 135 | >>> L = [9,7,8,3,2,4,1,6,5] |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame^] | 136 | >>> [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 | ... |
| 146 | black 4 |
| 147 | blue 2 |
| 148 | green 3 |
| 149 | red 1 |
| 150 | yellow 5 |
| 151 | |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 152 | \end{verbatim} |
| 153 | |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 154 | |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame^] | 155 | \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. Kuchling | 6aedcfc | 2003-10-21 12:48:23 +0000 | [diff] [blame] | 169 | |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 170 | \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 | |
| 182 | The net result of the 2.4 optimizations is that Python 2.4 runs the |
| 183 | pystone benchmark around XX\% faster than Python 2.3 and YY\% faster |
| 184 | than Python 2.2. |
| 185 | |
| 186 | |
| 187 | %====================================================================== |
| 188 | \section{New, Improved, and Deprecated Modules} |
| 189 | |
| 190 | As usual, Python's standard library received a number of enhancements and |
| 191 | bug fixes. Here's a partial list of the most notable changes, sorted |
| 192 | alphabetically by module name. Consult the |
| 193 | \file{Misc/NEWS} file in the source tree for a more |
| 194 | complete list of changes, or look through the CVS logs for all the |
| 195 | details. |
| 196 | |
| 197 | \begin{itemize} |
| 198 | |
Andrew M. Kuchling | 69f31eb | 2003-08-13 23:11:04 +0000 | [diff] [blame] | 199 | \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. Kuchling | 6aedcfc | 2003-10-21 12:48:23 +0000 | [diff] [blame] | 203 | |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame^] | 204 | \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. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 207 | |
Andrew M. Kuchling | 6aedcfc | 2003-10-21 12:48:23 +0000 | [diff] [blame] | 208 | \item The \module{random} module has a new method called \method{getrandbits(N)} |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame^] | 209 | 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. Kuchling | 6aedcfc | 2003-10-21 12:48:23 +0000 | [diff] [blame] | 213 | |
| 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. Kuchling | 69f31eb | 2003-08-13 23:11:04 +0000 | [diff] [blame] | 221 | |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 222 | \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 | |
| 232 | Changes to Python's build process and to the C API include: |
| 233 | |
| 234 | \begin{itemize} |
| 235 | |
Andrew M. Kuchling | 6aedcfc | 2003-10-21 12:48:23 +0000 | [diff] [blame] | 236 | \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 Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 243 | |
| 244 | \end{itemize} |
| 245 | |
| 246 | |
| 247 | %====================================================================== |
| 248 | \subsection{Port-Specific Changes} |
| 249 | |
| 250 | Platform-specific changes go here. |
| 251 | |
| 252 | |
| 253 | %====================================================================== |
| 254 | \section{Other Changes and Fixes \label{section-other}} |
| 255 | |
| 256 | As usual, there were a bunch of other improvements and bugfixes |
| 257 | scattered throughout the source tree. A search through the CVS change |
| 258 | logs finds there were XXX patches applied and YYY bugs fixed between |
| 259 | Python 2.3 and 2.4. Both figures are likely to be underestimates. |
| 260 | |
| 261 | Some 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 | |
| 273 | This section lists previously described changes that may require |
| 274 | changes to your code: |
| 275 | |
| 276 | \begin{itemize} |
| 277 | |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame^] | 278 | \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. Kuchling | 6aedcfc | 2003-10-21 12:48:23 +0000 | [diff] [blame] | 281 | |
| 282 | \item \function{dircache.listdir()} now passes exceptions to the caller |
| 283 | instead of returning empty lists. |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 284 | |
| 285 | \end{itemize} |
| 286 | |
| 287 | |
| 288 | %====================================================================== |
| 289 | \section{Acknowledgements \label{acks}} |
| 290 | |
| 291 | The author would like to thank the following people for offering |
| 292 | suggestions, corrections and assistance with various drafts of this |
| 293 | article: . |
| 294 | |
| 295 | \end{document} |