Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 1 | \documentclass{howto} |
| 2 | \usepackage{distutils} |
| 3 | % $Id$ |
| 4 | |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 5 | |
| 6 | \title{What's New in Python 2.5} |
| 7 | \release{0.0} |
Andrew M. Kuchling | 92e2495 | 2004-12-03 13:54:09 +0000 | [diff] [blame] | 8 | \author{A.M. Kuchling} |
| 9 | \authoraddress{\email{amk@amk.ca}} |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 10 | |
| 11 | \begin{document} |
| 12 | \maketitle |
| 13 | \tableofcontents |
| 14 | |
| 15 | This article explains the new features in Python 2.5. No release date |
Andrew M. Kuchling | 92e2495 | 2004-12-03 13:54:09 +0000 | [diff] [blame] | 16 | for Python 2.5 has been set; it will probably be released in late 2005. |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 17 | |
| 18 | % Compare with previous release in 2 - 3 sentences here. |
| 19 | |
| 20 | This article doesn't attempt to provide a complete specification of |
| 21 | the new features, but instead provides a convenient overview. For |
| 22 | full details, you should refer to the documentation for Python 2.5. |
| 23 | % add hyperlink when the documentation becomes available online. |
| 24 | If you want to understand the complete implementation and design |
| 25 | rationale, refer to the PEP for a particular new feature. |
| 26 | |
| 27 | |
| 28 | %====================================================================== |
Andrew M. Kuchling | 3e41b05 | 2005-03-01 00:53:46 +0000 | [diff] [blame] | 29 | \section{PEP 309: Partial Function Application} |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 30 | |
Andrew M. Kuchling | b1c96fd | 2005-03-20 21:42:04 +0000 | [diff] [blame] | 31 | The \module{functional} module is intended to contain tools for |
| 32 | functional-style programming. Currently it only contains |
| 33 | \class{partial}, but new functions will probably be added in future |
| 34 | versions of Python. |
| 35 | |
Andrew M. Kuchling | 4b000cd | 2005-04-09 15:51:44 +0000 | [diff] [blame] | 36 | For programs written in a functional style, it can be useful to |
| 37 | construct variants of existing functions that have some of the |
| 38 | parameters filled in. Consider a Python function \code{f(a, b, c)}; |
| 39 | you could create a new function \code{g(b, c)} that was equivalent to |
| 40 | \code{f(1, b, c)}. This is called ``partial function application'', |
| 41 | and is provided by the \class{partial} class in the new |
| 42 | \module{functional} module. |
| 43 | |
| 44 | The constructor for \class{partial} takes the arguments |
| 45 | \code{(\var{function}, \var{arg1}, \var{arg2}, ... |
| 46 | \var{kwarg1}=\var{value1}, \var{kwarg2}=\var{value2})}. The resulting |
| 47 | object is callable, so you can just call it to invoke \var{function} |
| 48 | with the filled-in arguments. |
| 49 | |
| 50 | Here's a small but realistic example: |
| 51 | |
| 52 | \begin{verbatim} |
| 53 | import functional |
| 54 | |
| 55 | def log (message, subsystem): |
| 56 | "Write the contents of 'message' to the specified subsystem." |
| 57 | print '%s: %s' % (subsystem, message) |
| 58 | ... |
| 59 | |
| 60 | server_log = functional.partial(log, subsystem='server') |
| 61 | \end{verbatim} |
| 62 | |
Andrew M. Kuchling | 6af7fe0 | 2005-08-02 17:20:36 +0000 | [diff] [blame^] | 63 | Here's another example, from a program that uses PyGTk. Here a |
| 64 | context-sensitive pop-up menu is being constructed dynamically. The |
| 65 | callback provided for the menu option is a partially applied version |
| 66 | of the \method{open_item()} method, where the first argument has been |
| 67 | provided. |
Andrew M. Kuchling | 4b000cd | 2005-04-09 15:51:44 +0000 | [diff] [blame] | 68 | |
Andrew M. Kuchling | 6af7fe0 | 2005-08-02 17:20:36 +0000 | [diff] [blame^] | 69 | \begin{verbatim} |
| 70 | ... |
| 71 | class Application: |
| 72 | def open_item(self, path): |
| 73 | ... |
| 74 | def init (self): |
| 75 | open_func = functional.partial(self.open_item, item_path) |
| 76 | popup_menu.append( ("Open", open_func, 1) ) |
| 77 | \end{verbatim} |
Andrew M. Kuchling | b1c96fd | 2005-03-20 21:42:04 +0000 | [diff] [blame] | 78 | |
| 79 | |
| 80 | \begin{seealso} |
| 81 | |
| 82 | \seepep{309}{Partial Function Application}{PEP proposed and written by |
| 83 | Peter Harris; implemented by Hye-Shik Chang, with adaptations by |
| 84 | Raymond Hettinger.} |
| 85 | |
| 86 | \end{seealso} |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 87 | |
| 88 | |
| 89 | %====================================================================== |
Fred Drake | db7b002 | 2005-03-20 22:19:47 +0000 | [diff] [blame] | 90 | \section{PEP 314: Metadata for Python Software Packages v1.1} |
| 91 | |
Andrew M. Kuchling | d8d732e | 2005-04-09 23:59:41 +0000 | [diff] [blame] | 92 | Some simple dependency support was added to Distutils. The |
| 93 | \function{setup()} function now has \code{requires},\code{provides}, |
| 94 | and \code{obsoletes}. When you build a source distribution using the |
| 95 | \code{sdist} command, the dependency information will be recorded in |
| 96 | the \file{PKG-INFO} file. |
| 97 | |
| 98 | Another new keyword is \code{download_url}, which should be set to a |
| 99 | URL for the package's source code. This means it's now possible to |
| 100 | look up an entry in the package index, determine the dependencies for |
| 101 | a package, and download the required packages. |
| 102 | |
| 103 | % XXX put example here |
| 104 | |
| 105 | \begin{seealso} |
| 106 | |
| 107 | \seepep{314}{Metadata for Python Software Packages v1.1}{PEP proposed |
| 108 | and written by A.M. Kuchling, Richard Jones, and Fred Drake; |
| 109 | implemented by Richard Jones and Fred Drake.} |
| 110 | |
| 111 | \end{seealso} |
Fred Drake | db7b002 | 2005-03-20 22:19:47 +0000 | [diff] [blame] | 112 | |
| 113 | |
| 114 | %====================================================================== |
Andrew M. Kuchling | a2e21cb | 2005-08-02 17:13:21 +0000 | [diff] [blame] | 115 | \section{PEP 342: New Generator Features} |
| 116 | |
| 117 | XXX write this section |
| 118 | |
| 119 | \begin{seealso} |
| 120 | |
| 121 | \seepep{342}{Coroutines via Enhanced Generators}{PEP written by |
| 122 | Guido van Rossum and Phillip J. Eby; |
| 123 | implemented by Phillip J. Eby.} |
| 124 | |
| 125 | \end{seealso} |
| 126 | |
| 127 | |
| 128 | %====================================================================== |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 129 | \section{Other Language Changes} |
| 130 | |
| 131 | Here are all of the changes that Python 2.5 makes to the core Python |
| 132 | language. |
| 133 | |
| 134 | \begin{itemize} |
Andrew M. Kuchling | 1cae3f5 | 2004-12-03 14:57:21 +0000 | [diff] [blame] | 135 | |
| 136 | \item The \function{min()} and \function{max()} built-in functions |
| 137 | gained a \code{key} keyword argument analogous to the \code{key} |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 138 | argument for \method{sort()}. This argument supplies a function |
Andrew M. Kuchling | 1cae3f5 | 2004-12-03 14:57:21 +0000 | [diff] [blame] | 139 | that takes a single argument and is called for every value in the list; |
| 140 | \function{min()}/\function{max()} will return the element with the |
| 141 | smallest/largest return value from this function. |
| 142 | For example, to find the longest string in a list, you can do: |
| 143 | |
| 144 | \begin{verbatim} |
| 145 | L = ['medium', 'longest', 'short'] |
| 146 | # Prints 'longest' |
| 147 | print max(L, key=len) |
| 148 | # Prints 'short', because lexicographically 'short' has the largest value |
| 149 | print max(L) |
| 150 | \end{verbatim} |
| 151 | |
| 152 | (Contributed by Steven Bethard and Raymond Hettinger.) |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 153 | |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 154 | \item The list of base classes in a class definition can now be empty. |
| 155 | As an example, this is now legal: |
| 156 | |
| 157 | \begin{verbatim} |
| 158 | class C(): |
| 159 | pass |
| 160 | \end{verbatim} |
| 161 | (Implemented by Brett Cannon.) |
| 162 | |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 163 | \end{itemize} |
| 164 | |
| 165 | |
| 166 | %====================================================================== |
| 167 | \subsection{Optimizations} |
| 168 | |
| 169 | \begin{itemize} |
| 170 | |
| 171 | \item Optimizations should be described here. |
| 172 | |
| 173 | \end{itemize} |
| 174 | |
| 175 | The net result of the 2.5 optimizations is that Python 2.5 runs the |
Andrew M. Kuchling | 92e2495 | 2004-12-03 13:54:09 +0000 | [diff] [blame] | 176 | pystone benchmark around XX\% faster than Python 2.4. |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 177 | |
| 178 | |
| 179 | %====================================================================== |
| 180 | \section{New, Improved, and Deprecated Modules} |
| 181 | |
| 182 | As usual, Python's standard library received a number of enhancements and |
| 183 | bug fixes. Here's a partial list of the most notable changes, sorted |
| 184 | alphabetically by module name. Consult the |
| 185 | \file{Misc/NEWS} file in the source tree for a more |
| 186 | complete list of changes, or look through the CVS logs for all the |
| 187 | details. |
| 188 | |
| 189 | \begin{itemize} |
| 190 | |
Andrew M. Kuchling | 3e41b05 | 2005-03-01 00:53:46 +0000 | [diff] [blame] | 191 | % the cPickle module no longer accepts the deprecated None option in the |
| 192 | % args tuple returned by __reduce__(). |
| 193 | |
| 194 | % csv module improvements |
| 195 | |
| 196 | % datetime.datetime() now has a strptime class method which can be used to |
| 197 | % create datetime object using a string and format. |
| 198 | |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 199 | \item The \function{nsmallest()} and |
| 200 | \function{nlargest()} functions in the \module{heapq} module |
| 201 | now support a \code{key} keyword argument similar to the one |
| 202 | provided by the \function{min()}/\function{max()} functions |
| 203 | and the \method{sort()} methods. For example: |
| 204 | Example: |
| 205 | |
| 206 | \begin{verbatim} |
| 207 | >>> import heapq |
| 208 | >>> L = ["short", 'medium', 'longest', 'longer still'] |
| 209 | >>> heapq.nsmallest(2, L) # Return two lowest elements, lexicographically |
| 210 | ['longer still', 'longest'] |
| 211 | >>> heapq.nsmallest(2, L, key=len) # Return two shortest elements |
| 212 | ['short', 'medium'] |
| 213 | \end{verbatim} |
| 214 | |
| 215 | (Contributed by Raymond Hettinger.) |
| 216 | |
Andrew M. Kuchling | 511a3a8 | 2005-03-20 19:52:18 +0000 | [diff] [blame] | 217 | \item The \function{itertools.islice()} function now accepts |
| 218 | \code{None} for the start and step arguments. This makes it more |
| 219 | compatible with the attributes of slice objects, so that you can now write |
| 220 | the following: |
| 221 | |
| 222 | \begin{verbatim} |
| 223 | s = slice(5) # Create slice object |
| 224 | itertools.islice(iterable, s.start, s.stop, s.step) |
| 225 | \end{verbatim} |
| 226 | |
| 227 | (Contributed by Raymond Hettinger.) |
Andrew M. Kuchling | 3e41b05 | 2005-03-01 00:53:46 +0000 | [diff] [blame] | 228 | |
| 229 | \item New module: \module{spwd} provides functions for accessing the |
| 230 | shadow password database on systems that support it. |
| 231 | % XXX give example |
| 232 | |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 233 | \item The \module{os} module underwent a number of changes. The |
| 234 | \member{stat_float_times} variable now defaults to true, meaning that |
| 235 | \function{os.stat()} will now return time values as floats. (This |
| 236 | doesn't necessarily mean that \function{os.stat()} will return times |
| 237 | that are precise to fractions of a second; not all systems support |
| 238 | such precision.) |
Andrew M. Kuchling | 3e41b05 | 2005-03-01 00:53:46 +0000 | [diff] [blame] | 239 | |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 240 | Also, constants named \member{os.SEEK_SET}, \member{os.SEEK_CUR}, and |
| 241 | \member{os.SEEK_END} have been added; these are the parameters to the |
| 242 | \function{os.lseek()} function. |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 243 | |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 244 | \item The \class{TarFile} class in the \module{tarfile} module now has |
Georg Brandl | 08c02db | 2005-07-22 18:39:19 +0000 | [diff] [blame] | 245 | an \method{extractall()} method that extracts all members from the |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 246 | archive into the current working directory. It's also possible to set |
| 247 | a different directory as the extraction target, and to unpack only a |
| 248 | subset of the archive's members. (Contributed by Lars Gust\"abel.) |
| 249 | |
Fred Drake | 114b8ca | 2005-03-21 05:47:11 +0000 | [diff] [blame] | 250 | \end{itemize} |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 251 | |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 252 | |
| 253 | |
| 254 | %====================================================================== |
| 255 | % whole new modules get described in \subsections here |
| 256 | |
| 257 | |
| 258 | % ====================================================================== |
| 259 | \section{Build and C API Changes} |
| 260 | |
| 261 | Changes to Python's build process and to the C API include: |
| 262 | |
| 263 | \begin{itemize} |
| 264 | |
Andrew M. Kuchling | 2238fc6 | 2004-12-03 15:16:40 +0000 | [diff] [blame] | 265 | \item The \cfunction{PyRange_New()} function was removed. It was never documented, |
| 266 | never used in the core code, and had dangerously lax error checking. |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 267 | |
| 268 | \end{itemize} |
| 269 | |
| 270 | |
| 271 | %====================================================================== |
| 272 | \subsection{Port-Specific Changes} |
| 273 | |
| 274 | Platform-specific changes go here. |
| 275 | |
| 276 | |
| 277 | %====================================================================== |
| 278 | \section{Other Changes and Fixes \label{section-other}} |
| 279 | |
| 280 | As usual, there were a bunch of other improvements and bugfixes |
| 281 | scattered throughout the source tree. A search through the CVS change |
| 282 | logs finds there were XXX patches applied and YYY bugs fixed between |
Andrew M. Kuchling | 92e2495 | 2004-12-03 13:54:09 +0000 | [diff] [blame] | 283 | Python 2.4 and 2.5. Both figures are likely to be underestimates. |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 284 | |
| 285 | Some of the more notable changes are: |
| 286 | |
| 287 | \begin{itemize} |
| 288 | |
| 289 | \item Details go here. |
| 290 | |
| 291 | \end{itemize} |
| 292 | |
| 293 | |
| 294 | %====================================================================== |
| 295 | \section{Porting to Python 2.5} |
| 296 | |
| 297 | This section lists previously described changes that may require |
| 298 | changes to your code: |
| 299 | |
| 300 | \begin{itemize} |
| 301 | |
Andrew M. Kuchling | 3e41b05 | 2005-03-01 00:53:46 +0000 | [diff] [blame] | 302 | \item Some old deprecated modules (\module{statcache}, \module{tzparse}, |
| 303 | \module{whrandom}) have been moved to \file{Lib/lib-old}. |
Andrew M. Kuchling | 0c35db9 | 2005-03-20 20:06:49 +0000 | [diff] [blame] | 304 | You can get access to these modules again by adding the directory |
| 305 | to your \code{sys.path}: |
| 306 | |
| 307 | \begin{verbatim} |
| 308 | import os |
| 309 | from distutils import sysconfig |
| 310 | |
| 311 | lib_dir = sysconfig.get_python_lib(standard_lib=True) |
| 312 | old_dir = os.path.join(lib_dir, 'lib-old') |
| 313 | sys.path.append(old_dir) |
| 314 | \end{verbatim} |
| 315 | |
| 316 | Doing so is discouraged, however; it's better to update any code that |
| 317 | still uses these modules. |
Andrew M. Kuchling | 3e41b05 | 2005-03-01 00:53:46 +0000 | [diff] [blame] | 318 | |
| 319 | % the pickle module no longer uses the deprecated bin parameter. |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 320 | |
| 321 | \end{itemize} |
| 322 | |
| 323 | |
| 324 | %====================================================================== |
| 325 | \section{Acknowledgements \label{acks}} |
| 326 | |
| 327 | The author would like to thank the following people for offering |
| 328 | suggestions, corrections and assistance with various drafts of this |
| 329 | article: . |
| 330 | |
| 331 | \end{document} |