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 | |
| 58 | |
| 59 | %====================================================================== |
| 60 | \section{Other Language Changes} |
| 61 | |
| 62 | Here are all of the changes that Python 2.4 makes to the core Python |
| 63 | language. |
| 64 | |
| 65 | \begin{itemize} |
Andrew M. Kuchling | 2fb4d51 | 2003-10-21 12:31:16 +0000 | [diff] [blame] | 66 | \item The \method{sort()} method of lists gained three keyword |
| 67 | arguments, \var{cmp}, \var{key}, and \var{reverse}. These arguments |
| 68 | make 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 |
| 72 | function that takes two arguments and returns -1, 0, or +1 depending |
| 73 | on how the arguments compare. |
| 74 | |
| 75 | \var{key} should be a single-argument function that takes a list |
| 76 | element and returns a comparison key for the element. The list is |
| 77 | then sorted using the comparison keys. The following example sorts a list |
| 78 | case-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 | |
| 93 | The last example, which uses the \var{cmp} parameter, is the old way |
| 94 | to perform a case-insensitive sort. It works, but is slower than |
| 95 | using 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 | |
| 99 | The \var{reverse} parameter should have a Boolean value. If the value is |
| 100 | \constant{True}, the list will be sorted into reverse order. Instead |
| 101 | of \code{L.sort() ; L.reverse()}, you can now write |
| 102 | \code{L.sort(reverse=True)}. |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 103 | |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame^] | 104 | \item The list type gained a \method{sorted(iterable)} method that |
| 105 | returns the elements of the iterable as a sorted list. It also accepts |
| 106 | the \var{cmp}, \var{key}, and \var{reverse} keyword arguments, same as |
| 107 | the \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 | |
| 118 | Note that the original list is unchanged; the list returned by |
| 119 | \method{sorted()} is a newly-created one. |
| 120 | |
Andrew M. Kuchling | 6aedcfc | 2003-10-21 12:48:23 +0000 | [diff] [blame] | 121 | \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 Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 125 | \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 | |
| 137 | The net result of the 2.4 optimizations is that Python 2.4 runs the |
| 138 | pystone benchmark around XX\% faster than Python 2.3 and YY\% faster |
| 139 | than Python 2.2. |
| 140 | |
| 141 | |
| 142 | %====================================================================== |
| 143 | \section{New, Improved, and Deprecated Modules} |
| 144 | |
| 145 | As usual, Python's standard library received a number of enhancements and |
| 146 | bug fixes. Here's a partial list of the most notable changes, sorted |
| 147 | alphabetically by module name. Consult the |
| 148 | \file{Misc/NEWS} file in the source tree for a more |
| 149 | complete list of changes, or look through the CVS logs for all the |
| 150 | details. |
| 151 | |
| 152 | \begin{itemize} |
| 153 | |
Andrew M. Kuchling | 69f31eb | 2003-08-13 23:11:04 +0000 | [diff] [blame] | 154 | \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. Kuchling | 6aedcfc | 2003-10-21 12:48:23 +0000 | [diff] [blame] | 158 | |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame^] | 159 | \item The \module{heapq} module is no longer implemented in Python, |
| 160 | having been converted into C. |
| 161 | |
Andrew M. Kuchling | 6aedcfc | 2003-10-21 12:48:23 +0000 | [diff] [blame] | 162 | \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. Kuchling | 69f31eb | 2003-08-13 23:11:04 +0000 | [diff] [blame] | 172 | |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 173 | \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 | |
| 183 | Changes to Python's build process and to the C API include: |
| 184 | |
| 185 | \begin{itemize} |
| 186 | |
Andrew M. Kuchling | 6aedcfc | 2003-10-21 12:48:23 +0000 | [diff] [blame] | 187 | \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 Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 194 | |
| 195 | \end{itemize} |
| 196 | |
| 197 | |
| 198 | %====================================================================== |
| 199 | \subsection{Port-Specific Changes} |
| 200 | |
| 201 | Platform-specific changes go here. |
| 202 | |
| 203 | |
| 204 | %====================================================================== |
| 205 | \section{Other Changes and Fixes \label{section-other}} |
| 206 | |
| 207 | As usual, there were a bunch of other improvements and bugfixes |
| 208 | scattered throughout the source tree. A search through the CVS change |
| 209 | logs finds there were XXX patches applied and YYY bugs fixed between |
| 210 | Python 2.3 and 2.4. Both figures are likely to be underestimates. |
| 211 | |
| 212 | Some 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 | |
| 224 | This section lists previously described changes that may require |
| 225 | changes to your code: |
| 226 | |
| 227 | \begin{itemize} |
| 228 | |
Andrew M. Kuchling | 6aedcfc | 2003-10-21 12:48:23 +0000 | [diff] [blame] | 229 | \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 Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 235 | |
| 236 | \end{itemize} |
| 237 | |
| 238 | |
| 239 | %====================================================================== |
| 240 | \section{Acknowledgements \label{acks}} |
| 241 | |
| 242 | The author would like to thank the following people for offering |
| 243 | suggestions, corrections and assistance with various drafts of this |
| 244 | article: . |
| 245 | |
| 246 | \end{document} |