blob: 3b07b10f5b16b5076559f64d96679f7b2b7c3e46 [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
63Here's another example, from a program that uses PyGTk.
64
Andrew M. Kuchlingb1c96fd2005-03-20 21:42:04 +000065% XXX add example from my GTk programming
66
67
68\begin{seealso}
69
70\seepep{309}{Partial Function Application}{PEP proposed and written by
71Peter Harris; implemented by Hye-Shik Chang, with adaptations by
72Raymond Hettinger.}
73
74\end{seealso}
Fred Drake2db76802004-12-01 05:05:47 +000075
76
77%======================================================================
Fred Drakedb7b0022005-03-20 22:19:47 +000078\section{PEP 314: Metadata for Python Software Packages v1.1}
79
Andrew M. Kuchlingd8d732e2005-04-09 23:59:41 +000080Some simple dependency support was added to Distutils. The
81\function{setup()} function now has \code{requires},\code{provides},
82and \code{obsoletes}. When you build a source distribution using the
83\code{sdist} command, the dependency information will be recorded in
84the \file{PKG-INFO} file.
85
86Another new keyword is \code{download_url}, which should be set to a
87URL for the package's source code. This means it's now possible to
88look up an entry in the package index, determine the dependencies for
89a package, and download the required packages.
90
91% XXX put example here
92
93\begin{seealso}
94
95\seepep{314}{Metadata for Python Software Packages v1.1}{PEP proposed
96and written by A.M. Kuchling, Richard Jones, and Fred Drake;
97implemented by Richard Jones and Fred Drake.}
98
99\end{seealso}
Fred Drakedb7b0022005-03-20 22:19:47 +0000100
101
102%======================================================================
Andrew M. Kuchlinga2e21cb2005-08-02 17:13:21 +0000103\section{PEP 342: New Generator Features}
104
105XXX write this section
106
107\begin{seealso}
108
109\seepep{342}{Coroutines via Enhanced Generators}{PEP written by
110Guido van Rossum and Phillip J. Eby;
111implemented by Phillip J. Eby.}
112
113\end{seealso}
114
115
116%======================================================================
Fred Drake2db76802004-12-01 05:05:47 +0000117\section{Other Language Changes}
118
119Here are all of the changes that Python 2.5 makes to the core Python
120language.
121
122\begin{itemize}
Andrew M. Kuchling1cae3f52004-12-03 14:57:21 +0000123
124\item The \function{min()} and \function{max()} built-in functions
125gained a \code{key} keyword argument analogous to the \code{key}
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000126argument for \method{sort()}. This argument supplies a function
Andrew M. Kuchling1cae3f52004-12-03 14:57:21 +0000127that takes a single argument and is called for every value in the list;
128\function{min()}/\function{max()} will return the element with the
129smallest/largest return value from this function.
130For example, to find the longest string in a list, you can do:
131
132\begin{verbatim}
133L = ['medium', 'longest', 'short']
134# Prints 'longest'
135print max(L, key=len)
136# Prints 'short', because lexicographically 'short' has the largest value
137print max(L)
138\end{verbatim}
139
140(Contributed by Steven Bethard and Raymond Hettinger.)
Fred Drake2db76802004-12-01 05:05:47 +0000141
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000142\item The list of base classes in a class definition can now be empty.
143As an example, this is now legal:
144
145\begin{verbatim}
146class C():
147 pass
148\end{verbatim}
149(Implemented by Brett Cannon.)
150
Fred Drake2db76802004-12-01 05:05:47 +0000151\end{itemize}
152
153
154%======================================================================
155\subsection{Optimizations}
156
157\begin{itemize}
158
159\item Optimizations should be described here.
160
161\end{itemize}
162
163The net result of the 2.5 optimizations is that Python 2.5 runs the
Andrew M. Kuchling92e24952004-12-03 13:54:09 +0000164pystone benchmark around XX\% faster than Python 2.4.
Fred Drake2db76802004-12-01 05:05:47 +0000165
166
167%======================================================================
168\section{New, Improved, and Deprecated Modules}
169
170As usual, Python's standard library received a number of enhancements and
171bug fixes. Here's a partial list of the most notable changes, sorted
172alphabetically by module name. Consult the
173\file{Misc/NEWS} file in the source tree for a more
174complete list of changes, or look through the CVS logs for all the
175details.
176
177\begin{itemize}
178
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +0000179% the cPickle module no longer accepts the deprecated None option in the
180% args tuple returned by __reduce__().
181
182% csv module improvements
183
184% datetime.datetime() now has a strptime class method which can be used to
185% create datetime object using a string and format.
186
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000187\item The \function{nsmallest()} and
188\function{nlargest()} functions in the \module{heapq} module
189now support a \code{key} keyword argument similar to the one
190provided by the \function{min()}/\function{max()} functions
191and the \method{sort()} methods. For example:
192Example:
193
194\begin{verbatim}
195>>> import heapq
196>>> L = ["short", 'medium', 'longest', 'longer still']
197>>> heapq.nsmallest(2, L) # Return two lowest elements, lexicographically
198['longer still', 'longest']
199>>> heapq.nsmallest(2, L, key=len) # Return two shortest elements
200['short', 'medium']
201\end{verbatim}
202
203(Contributed by Raymond Hettinger.)
204
Andrew M. Kuchling511a3a82005-03-20 19:52:18 +0000205\item The \function{itertools.islice()} function now accepts
206\code{None} for the start and step arguments. This makes it more
207compatible with the attributes of slice objects, so that you can now write
208the following:
209
210\begin{verbatim}
211s = slice(5) # Create slice object
212itertools.islice(iterable, s.start, s.stop, s.step)
213\end{verbatim}
214
215(Contributed by Raymond Hettinger.)
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +0000216
217\item New module: \module{spwd} provides functions for accessing the
218shadow password database on systems that support it.
219% XXX give example
220
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000221\item The \module{os} module underwent a number of changes. The
222\member{stat_float_times} variable now defaults to true, meaning that
223\function{os.stat()} will now return time values as floats. (This
224doesn't necessarily mean that \function{os.stat()} will return times
225that are precise to fractions of a second; not all systems support
226such precision.)
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +0000227
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000228Also, constants named \member{os.SEEK_SET}, \member{os.SEEK_CUR}, and
229\member{os.SEEK_END} have been added; these are the parameters to the
230\function{os.lseek()} function.
Fred Drake2db76802004-12-01 05:05:47 +0000231
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000232\item The \class{TarFile} class in the \module{tarfile} module now has
Georg Brandl08c02db2005-07-22 18:39:19 +0000233an \method{extractall()} method that extracts all members from the
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000234archive into the current working directory. It's also possible to set
235a different directory as the extraction target, and to unpack only a
236subset of the archive's members. (Contributed by Lars Gust\"abel.)
237
Fred Drake114b8ca2005-03-21 05:47:11 +0000238\end{itemize}
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000239
Fred Drake2db76802004-12-01 05:05:47 +0000240
241
242%======================================================================
243% whole new modules get described in \subsections here
244
245
246% ======================================================================
247\section{Build and C API Changes}
248
249Changes to Python's build process and to the C API include:
250
251\begin{itemize}
252
Andrew M. Kuchling2238fc62004-12-03 15:16:40 +0000253\item The \cfunction{PyRange_New()} function was removed. It was never documented,
254never used in the core code, and had dangerously lax error checking.
Fred Drake2db76802004-12-01 05:05:47 +0000255
256\end{itemize}
257
258
259%======================================================================
260\subsection{Port-Specific Changes}
261
262Platform-specific changes go here.
263
264
265%======================================================================
266\section{Other Changes and Fixes \label{section-other}}
267
268As usual, there were a bunch of other improvements and bugfixes
269scattered throughout the source tree. A search through the CVS change
270logs finds there were XXX patches applied and YYY bugs fixed between
Andrew M. Kuchling92e24952004-12-03 13:54:09 +0000271Python 2.4 and 2.5. Both figures are likely to be underestimates.
Fred Drake2db76802004-12-01 05:05:47 +0000272
273Some of the more notable changes are:
274
275\begin{itemize}
276
277\item Details go here.
278
279\end{itemize}
280
281
282%======================================================================
283\section{Porting to Python 2.5}
284
285This section lists previously described changes that may require
286changes to your code:
287
288\begin{itemize}
289
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +0000290\item Some old deprecated modules (\module{statcache}, \module{tzparse},
291 \module{whrandom}) have been moved to \file{Lib/lib-old}.
Andrew M. Kuchling0c35db92005-03-20 20:06:49 +0000292You can get access to these modules again by adding the directory
293to your \code{sys.path}:
294
295\begin{verbatim}
296import os
297from distutils import sysconfig
298
299lib_dir = sysconfig.get_python_lib(standard_lib=True)
300old_dir = os.path.join(lib_dir, 'lib-old')
301sys.path.append(old_dir)
302\end{verbatim}
303
304Doing so is discouraged, however; it's better to update any code that
305still uses these modules.
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +0000306
307% the pickle module no longer uses the deprecated bin parameter.
Fred Drake2db76802004-12-01 05:05:47 +0000308
309\end{itemize}
310
311
312%======================================================================
313\section{Acknowledgements \label{acks}}
314
315The author would like to thank the following people for offering
316suggestions, corrections and assistance with various drafts of this
317article: .
318
319\end{document}