blob: 74cbf766cf20b2c447aa743f1bcbf7f9c48421c3 [file] [log] [blame]
Fred Drake2db76802004-12-01 05:05:47 +00001\documentclass{howto}
2\usepackage{distutils}
3% $Id$
4
Fred Drake2db76802004-12-01 05:05:47 +00005
6\title{What's New in Python 2.5}
7\release{0.0}
Andrew M. Kuchling92e24952004-12-03 13:54:09 +00008\author{A.M. Kuchling}
9\authoraddress{\email{amk@amk.ca}}
Fred Drake2db76802004-12-01 05:05:47 +000010
11\begin{document}
12\maketitle
13\tableofcontents
14
15This article explains the new features in Python 2.5. No release date
Andrew M. Kuchling92e24952004-12-03 13:54:09 +000016for Python 2.5 has been set; it will probably be released in late 2005.
Fred Drake2db76802004-12-01 05:05:47 +000017
18% Compare with previous release in 2 - 3 sentences here.
19
20This article doesn't attempt to provide a complete specification of
21the new features, but instead provides a convenient overview. For
22full details, you should refer to the documentation for Python 2.5.
23% add hyperlink when the documentation becomes available online.
24If you want to understand the complete implementation and design
25rationale, refer to the PEP for a particular new feature.
26
27
28%======================================================================
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +000029\section{PEP 309: Partial Function Application}
Fred Drake2db76802004-12-01 05:05:47 +000030
Andrew M. Kuchlingb1c96fd2005-03-20 21:42:04 +000031The \module{functional} module is intended to contain tools for
32functional-style programming. Currently it only contains
33\class{partial}, but new functions will probably be added in future
34versions of Python.
35
Andrew M. Kuchling4b000cd2005-04-09 15:51:44 +000036For programs written in a functional style, it can be useful to
37construct variants of existing functions that have some of the
38parameters filled in. Consider a Python function \code{f(a, b, c)};
39you 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'',
41and is provided by the \class{partial} class in the new
42\module{functional} module.
43
44The 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
47object is callable, so you can just call it to invoke \var{function}
48with the filled-in arguments.
49
50Here's a small but realistic example:
51
52\begin{verbatim}
53import functional
54
55def log (message, subsystem):
56 "Write the contents of 'message' to the specified subsystem."
57 print '%s: %s' % (subsystem, message)
58 ...
59
60server_log = functional.partial(log, subsystem='server')
61\end{verbatim}
62
Andrew M. Kuchling6af7fe02005-08-02 17:20:36 +000063Here's another example, from a program that uses PyGTk. Here a
64context-sensitive pop-up menu is being constructed dynamically. The
65callback provided for the menu option is a partially applied version
66of the \method{open_item()} method, where the first argument has been
67provided.
Andrew M. Kuchling4b000cd2005-04-09 15:51:44 +000068
Andrew M. Kuchling6af7fe02005-08-02 17:20:36 +000069\begin{verbatim}
70...
71class 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. Kuchlingb1c96fd2005-03-20 21:42:04 +000078
79
80\begin{seealso}
81
82\seepep{309}{Partial Function Application}{PEP proposed and written by
83Peter Harris; implemented by Hye-Shik Chang, with adaptations by
84Raymond Hettinger.}
85
86\end{seealso}
Fred Drake2db76802004-12-01 05:05:47 +000087
88
89%======================================================================
Fred Drakedb7b0022005-03-20 22:19:47 +000090\section{PEP 314: Metadata for Python Software Packages v1.1}
91
Andrew M. Kuchlingd8d732e2005-04-09 23:59:41 +000092Some simple dependency support was added to Distutils. The
93\function{setup()} function now has \code{requires},\code{provides},
94and \code{obsoletes}. When you build a source distribution using the
95\code{sdist} command, the dependency information will be recorded in
96the \file{PKG-INFO} file.
97
98Another new keyword is \code{download_url}, which should be set to a
99URL for the package's source code. This means it's now possible to
100look up an entry in the package index, determine the dependencies for
101a 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
108and written by A.M. Kuchling, Richard Jones, and Fred Drake;
109implemented by Richard Jones and Fred Drake.}
110
111\end{seealso}
Fred Drakedb7b0022005-03-20 22:19:47 +0000112
113
114%======================================================================
Andrew M. Kuchlinga2e21cb2005-08-02 17:13:21 +0000115\section{PEP 342: New Generator Features}
116
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000117As introduced in Python 2.3, generators only produce output; once a
118generator's code was invoked to create an iterator, there's no way to
119pass new parameters into the function when its execution is resumed.
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000120Hackish solutions to this include making the generator's code look at
121a global variable and then changing the global variable's value, or
122passing in some mutable object that callers then modify. Python
1232.5 adds the ability to pass values \emph{into} a generator.
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000124
125To refresh your memory of basic generators, here's a simple example:
126
127\begin{verbatim}
128def counter (maximum):
129 i = 0
130 while i < maximum:
131 yield i
132 i += 1
133\end{verbatim}
134
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000135When you call \code{counter(10)}, the result is an iterator that
136returns the values from 0 up to 9. On encountering the
137\keyword{yield} statement, the iterator returns the provided value and
138suspends the function's execution, preserving the local variables.
139Execution resumes on the following call to the iterator's
140\method{next()} method, picking up after the \keyword{yield}.
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000141
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000142In Python 2.3, \keyword{yield} was a statement; it didn't return any
143value. In 2.5, \keyword{yield} is now an expression, returning a
144value that can be assigned to a variable or otherwise operated on:
Andrew M. Kuchlinga2e21cb2005-08-02 17:13:21 +0000145
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000146\begin{verbatim}
147val = (yield i)
148\end{verbatim}
149
150I recommend that you always put parentheses around a \keyword{yield}
151expression when you're doing something with the returned value, as in
152the above example. The parentheses aren't always necessary, but it's
153easier to always add them instead of having to remember when they're
154needed. The exact rules are that a \keyword{yield}-expression must
155always be parenthesized except when it occurs at the top-level
156expression on the right-hand side of an assignment, meaning
157you can to write \code{val = yield i} but \code{val = (yield i) + 12}.
158
159Values are sent into a generator by calling its
160\method{send(\var{value})} method. The generator's code is then
161resumed and the \keyword{yield} expression produces \var{value}.
162If the regular \method{next()} method is called, the \keyword{yield}
163returns \constant{None}.
164
165Here's the previous example, modified to allow changing the value of
166the internal counter.
167
168\begin{verbatim}
169def counter (maximum):
170 i = 0
171 while i < maximum:
172 val = (yield i)
173 # If value provided, change counter
174 if val is not None:
175 i = val
176 else:
177 i += 1
178\end{verbatim}
179
180And here's an example of changing the counter:
181
182\begin{verbatim}
183>>> it = counter(10)
184>>> print it.next()
1850
186>>> print it.next()
1871
188>>> print it.send(8)
1898
190>>> print it.next()
1919
192>>> print it.next()
193Traceback (most recent call last):
194 File ``t.py'', line 15, in ?
195 print it.next()
196StopIteration
197
198Because \keyword{yield} will often be returning \constant{None},
199you shouldn't just use its value in expressions unless you're sure
200that only the \method{send()} method will be used.
201
202There are two other new methods on generators in addition to
203\method{send()}:
204
205\begin{itemize}
206
207 \item \method{throw(\var{type}, \var{value}=None,
208 \var{traceback}=None)} is used to raise an exception inside the
209 generator; the exception is raised by the \keyword{yield} expression
210 where the generator's execution is paused.
211
212 \item \method{close()} raises a new \exception{GeneratorExit}
213 exception inside the generator to terminate the iteration.
214 On receiving this
215 exception, the generator's code must either raise
216 \exception{GeneratorExit} or \exception{StopIteration}; catching the
217 exception and doing anything else is illegal and will trigger
218 a \exception{RuntimeError}. \method{close()} will also be called by
219 Python's garbage collection when the generator is garbage-collected.
220
221 If you need to run cleanup code in case of a \exception{GeneratorExit},
222 I suggest using a \code{try: ... finally:} suite instead of
223 catching \exception{GeneratorExit}.
224
225\end{itemize}
226
227The cumulative effect of these changes is to turn generators from
228one-way producers of information into both producers and consumers.
229Generators also become \emph{coroutines}, a more generalized form of
230subroutines; subroutines are entered at one point and exited at
231another point (the top of the function, and a \keyword{return
232statement}), but coroutines can be entered, exited, and resumed at
233many different points (the \keyword{yield} statements).science term
234
235
Andrew M. Kuchlinga2e21cb2005-08-02 17:13:21 +0000236\begin{seealso}
237
238\seepep{342}{Coroutines via Enhanced Generators}{PEP written by
239Guido van Rossum and Phillip J. Eby;
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000240implemented by Phillip J. Eby. Includes examples of
241some fancier uses of generators as coroutines.}
242
243\seeurl{http://en.wikipedia.org/wiki/Coroutine}{The Wikipedia entry for
244coroutines.}
245
246\seeurl{http://www.sidhe.org/~dan/blog/archives/000178.html}{An
247explanation of coroutines from a Perl point of view, written by Dan
248Sugalski.}
Andrew M. Kuchlinga2e21cb2005-08-02 17:13:21 +0000249
250\end{seealso}
251
252
253%======================================================================
Fred Drake2db76802004-12-01 05:05:47 +0000254\section{Other Language Changes}
255
256Here are all of the changes that Python 2.5 makes to the core Python
257language.
258
259\begin{itemize}
Andrew M. Kuchling1cae3f52004-12-03 14:57:21 +0000260
261\item The \function{min()} and \function{max()} built-in functions
262gained a \code{key} keyword argument analogous to the \code{key}
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000263argument for \method{sort()}. This argument supplies a function
Andrew M. Kuchling1cae3f52004-12-03 14:57:21 +0000264that takes a single argument and is called for every value in the list;
265\function{min()}/\function{max()} will return the element with the
266smallest/largest return value from this function.
267For example, to find the longest string in a list, you can do:
268
269\begin{verbatim}
270L = ['medium', 'longest', 'short']
271# Prints 'longest'
272print max(L, key=len)
273# Prints 'short', because lexicographically 'short' has the largest value
274print max(L)
275\end{verbatim}
276
277(Contributed by Steven Bethard and Raymond Hettinger.)
Fred Drake2db76802004-12-01 05:05:47 +0000278
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000279\item Two new built-in functions, \function{any()} and
280\function{all()}, evaluate whether an iterator contains any true or
281false values. \function{any()} returns \constant{True} if any value
282returned by the iterator is true; otherwise it will return
283\constant{False}. \function{all()} returns \constant{True} only if
284all of the values returned by the iterator evaluate as being true.
285
286% XXX who added?
287
288
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000289\item The list of base classes in a class definition can now be empty.
290As an example, this is now legal:
291
292\begin{verbatim}
293class C():
294 pass
295\end{verbatim}
296(Implemented by Brett Cannon.)
297
Fred Drake2db76802004-12-01 05:05:47 +0000298\end{itemize}
299
300
301%======================================================================
302\subsection{Optimizations}
303
304\begin{itemize}
305
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000306\item When they were introduced
307in Python 2.4, the built-in \class{set} and \class{frozenset} types
308were built on top of Python's dictionary type.
309In 2.5 the internal data structure has been customized for implementing sets,
310and as a result sets will use a third less memory and are somewhat faster.
311(Implemented by Raymond Hettinger.)
Fred Drake2db76802004-12-01 05:05:47 +0000312
313\end{itemize}
314
315The net result of the 2.5 optimizations is that Python 2.5 runs the
Andrew M. Kuchling92e24952004-12-03 13:54:09 +0000316pystone benchmark around XX\% faster than Python 2.4.
Fred Drake2db76802004-12-01 05:05:47 +0000317
318
319%======================================================================
320\section{New, Improved, and Deprecated Modules}
321
322As usual, Python's standard library received a number of enhancements and
323bug fixes. Here's a partial list of the most notable changes, sorted
324alphabetically by module name. Consult the
325\file{Misc/NEWS} file in the source tree for a more
326complete list of changes, or look through the CVS logs for all the
327details.
328
329\begin{itemize}
330
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000331% collections.deque now has .remove()
332
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +0000333% the cPickle module no longer accepts the deprecated None option in the
334% args tuple returned by __reduce__().
335
336% csv module improvements
337
338% datetime.datetime() now has a strptime class method which can be used to
339% create datetime object using a string and format.
340
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000341\item A new \module{hashlib} module has been added to replace the
342\module{md5} and \module{sha} modules. \module{hashlib} adds support
343for additional secure hashes (SHA-224, SHA-256, SHA-384, and SHA-512).
344When available, the module uses OpenSSL for fast platform optimized
345implementations of algorithms. The old \module{md5} and \module{sha}
346modules still exist as wrappers around hashlib to preserve backwards
347compatibility. (Contributed by Gregory P. Smith.)
348
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000349\item The \function{nsmallest()} and
350\function{nlargest()} functions in the \module{heapq} module
351now support a \code{key} keyword argument similar to the one
352provided by the \function{min()}/\function{max()} functions
353and the \method{sort()} methods. For example:
354Example:
355
356\begin{verbatim}
357>>> import heapq
358>>> L = ["short", 'medium', 'longest', 'longer still']
359>>> heapq.nsmallest(2, L) # Return two lowest elements, lexicographically
360['longer still', 'longest']
361>>> heapq.nsmallest(2, L, key=len) # Return two shortest elements
362['short', 'medium']
363\end{verbatim}
364
365(Contributed by Raymond Hettinger.)
366
Andrew M. Kuchling511a3a82005-03-20 19:52:18 +0000367\item The \function{itertools.islice()} function now accepts
368\code{None} for the start and step arguments. This makes it more
369compatible with the attributes of slice objects, so that you can now write
370the following:
371
372\begin{verbatim}
373s = slice(5) # Create slice object
374itertools.islice(iterable, s.start, s.stop, s.step)
375\end{verbatim}
376
377(Contributed by Raymond Hettinger.)
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +0000378
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000379\item The \module{operator} module's \function{itemgetter()}
380and \function{attrgetter()} functions now support multiple fields.
381A call such as \code{operator.attrgetter('a', 'b')}
382will return a function
383that retrieves the \member{a} and \member{b} attributes. Combining
384this new feature with the \method{sort()} method's \code{key} parameter
385lets you easily sort lists using multiple fields.
386
387% XXX who added?
388
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +0000389
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000390\item The \module{os} module underwent a number of changes. The
391\member{stat_float_times} variable now defaults to true, meaning that
392\function{os.stat()} will now return time values as floats. (This
393doesn't necessarily mean that \function{os.stat()} will return times
394that are precise to fractions of a second; not all systems support
395such precision.)
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +0000396
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000397Constants named \member{os.SEEK_SET}, \member{os.SEEK_CUR}, and
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000398\member{os.SEEK_END} have been added; these are the parameters to the
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000399\function{os.lseek()} function. Two new constants for locking are
400\member{os.O_SHLOCK} and \member{os.O_EXLOCK}.
401
402On FreeBSD, the \function{os.stat()} function now returns
403times with nanosecond resolution, and the returned object
404now has \member{st_gen} and \member{st_birthtime}.
405The \member{st_flags} member is also available, if the platform supports it.
406% XXX patch 1180695, 1212117
407
408\item New module: \module{spwd} provides functions for accessing the
409shadow password database on systems that support it.
410% XXX give example
Fred Drake2db76802004-12-01 05:05:47 +0000411
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000412\item The \class{TarFile} class in the \module{tarfile} module now has
Georg Brandl08c02db2005-07-22 18:39:19 +0000413an \method{extractall()} method that extracts all members from the
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000414archive into the current working directory. It's also possible to set
415a different directory as the extraction target, and to unpack only a
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000416subset of the archive's members.
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000417
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000418A tarfile's compression can be autodetected by
419using the mode \code{'r|*'}.
420% patch 918101
421(Contributed by Lars Gust\"abel.)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000422
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000423\item The \module{xmlrpclib} module now supports returning
424 \class{datetime} objects for the XML-RPC date type. Supply
425 \code{use_datetime=True} to the \function{loads()} function
426 or the \class{Unmarshaller} class to enable this feature.
427% XXX patch 1120353
428
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000429
Fred Drake114b8ca2005-03-21 05:47:11 +0000430\end{itemize}
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000431
Fred Drake2db76802004-12-01 05:05:47 +0000432
433
434%======================================================================
435% whole new modules get described in \subsections here
436
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000437% XXX new distutils features: upload
438
439
440
Fred Drake2db76802004-12-01 05:05:47 +0000441
442% ======================================================================
443\section{Build and C API Changes}
444
445Changes to Python's build process and to the C API include:
446
447\begin{itemize}
448
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000449\item The built-in set types now have an official C API. Call
450\cfunction{PySet_New()} and \cfunction{PyFrozenSet_New()} to create a
451new set, \cfunction{PySet_Add()} and \cfunction{PySet_Discard()} to
452add and remove elements, and \cfunction{PySet_Contains} and
453\cfunction{PySet_Size} to examine the set's state.
454
455\item The \cfunction{PyRange_New()} function was removed. It was
456never documented, never used in the core code, and had dangerously lax
457error checking.
Fred Drake2db76802004-12-01 05:05:47 +0000458
459\end{itemize}
460
461
462%======================================================================
463\subsection{Port-Specific Changes}
464
465Platform-specific changes go here.
466
467
468%======================================================================
469\section{Other Changes and Fixes \label{section-other}}
470
471As usual, there were a bunch of other improvements and bugfixes
472scattered throughout the source tree. A search through the CVS change
473logs finds there were XXX patches applied and YYY bugs fixed between
Andrew M. Kuchling92e24952004-12-03 13:54:09 +0000474Python 2.4 and 2.5. Both figures are likely to be underestimates.
Fred Drake2db76802004-12-01 05:05:47 +0000475
476Some of the more notable changes are:
477
478\begin{itemize}
479
480\item Details go here.
481
482\end{itemize}
483
484
485%======================================================================
486\section{Porting to Python 2.5}
487
488This section lists previously described changes that may require
489changes to your code:
490
491\begin{itemize}
492
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +0000493\item Some old deprecated modules (\module{statcache}, \module{tzparse},
494 \module{whrandom}) have been moved to \file{Lib/lib-old}.
Andrew M. Kuchling0c35db92005-03-20 20:06:49 +0000495You can get access to these modules again by adding the directory
496to your \code{sys.path}:
497
498\begin{verbatim}
499import os
500from distutils import sysconfig
501
502lib_dir = sysconfig.get_python_lib(standard_lib=True)
503old_dir = os.path.join(lib_dir, 'lib-old')
504sys.path.append(old_dir)
505\end{verbatim}
506
507Doing so is discouraged, however; it's better to update any code that
508still uses these modules.
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +0000509
510% the pickle module no longer uses the deprecated bin parameter.
Fred Drake2db76802004-12-01 05:05:47 +0000511
512\end{itemize}
513
514
515%======================================================================
516\section{Acknowledgements \label{acks}}
517
518The author would like to thank the following people for offering
519suggestions, corrections and assistance with various drafts of this
520article: .
521
522\end{document}