blob: c93f7d801acf0cee4f51542762673e646d069753 [file] [log] [blame]
Fred Drakeed0fa3d2003-07-30 19:14:09 +00001\documentclass{howto}
2\usepackage{distutils}
3% $Id$
4
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +00005% Don't write extensive text for new sections; I'll do that.
6% Feel free to add commented-out reminders of things that need
7% to be covered. --amk
8
Andrew M. Kuchlingd0b6d9d2004-07-04 15:35:00 +00009% XXX pydoc can display links to module docs -- but when?
10%
11
Fred Drakeed0fa3d2003-07-30 19:14:09 +000012\title{What's New in Python 2.4}
Andrew M. Kuchling3b790912004-07-04 16:39:40 +000013\release{0.1}
Fred Drakeed0fa3d2003-07-30 19:14:09 +000014\author{A.M.\ Kuchling}
Fred Drakeb914ef02004-01-02 06:57:50 +000015\authoraddress{
16 \strong{Python Software Foundation}\\
17 Email: \email{amk@amk.ca}
18}
Fred Drakeed0fa3d2003-07-30 19:14:09 +000019
20\begin{document}
21\maketitle
22\tableofcontents
23
Andrew M. Kuchling3b790912004-07-04 16:39:40 +000024This article explains the new features in Python 2.4 alpha1, to be released in early July 2004 The final version of Python 2.4
25is expected to be around September 2004.
Fred Drakeed0fa3d2003-07-30 19:14:09 +000026
Andrew M. Kuchling3b790912004-07-04 16:39:40 +000027Python 2.4 is a middle-sized release. It doesn't introduce as many
28changes as the radical Python 2.2, but introduces more features than
29the conservative 2.3 release did. The most significant new language
30feature (as of this writing) is the addition of generator expressions;
31most of the changes are to the standard library.
Fred Drakeed0fa3d2003-07-30 19:14:09 +000032
33This article doesn't attempt to provide a complete specification of
Andrew M. Kuchling3b790912004-07-04 16:39:40 +000034every single new feature, but instead provides a convenient overview.
35For full details, you should refer to the documentation for Python
362.4, such as the \citetitle[../lib/lib.html]{Python Library Reference}
37and the \citetitle[../ref/ref.html]{Python Reference Manual}. If you
38want to understand the complete implementation and design rationale,
39refer to the PEP for a particular new feature or to the module
40documentation.
Fred Drakeed0fa3d2003-07-30 19:14:09 +000041
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +000042
Raymond Hettinger7e0282f2003-11-24 07:14:54 +000043%======================================================================
44\section{PEP 218: Built-In Set Objects}
45
Fred Drake56fcc232004-05-06 02:55:35 +000046Two new built-in types, \function{set(\var{iterable})} and
47\function{frozenset(\var{iterable})} provide high speed data types for
Raymond Hettinger7e0282f2003-11-24 07:14:54 +000048membership testing, for eliminating duplicates from sequences, and
49for mathematical operations like unions, intersections, differences,
50and symmetric differences.
51
52\begin{verbatim}
53>>> a = set('abracadabra') # form a set from a string
54>>> 'z' in a # fast membership testing
55False
56>>> a # unique letters in a
57set(['a', 'r', 'b', 'c', 'd'])
58>>> ''.join(a) # convert back into a string
59'arbcd'
Raymond Hettingerd4462302003-11-26 17:52:45 +000060
Raymond Hettinger7e0282f2003-11-24 07:14:54 +000061>>> b = set('alacazam') # form a second set
62>>> a - b # letters in a but not in b
63set(['r', 'd', 'b'])
64>>> a | b # letters in either a or b
65set(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'])
66>>> a & b # letters in both a and b
67set(['a', 'c'])
68>>> a ^ b # letters in a or b but not both
69set(['r', 'd', 'b', 'm', 'z', 'l'])
Raymond Hettingerd4462302003-11-26 17:52:45 +000070
Raymond Hettinger7e0282f2003-11-24 07:14:54 +000071>>> a.add('z') # add a new element
72>>> a.update('wxy') # add multiple new elements
73>>> a
74set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'x', 'z'])
75>>> a.remove('x') # take one element out
76>>> a
77set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'z'])
78\end{verbatim}
79
80The type \function{frozenset()} is an immutable version of \function{set()}.
81Since it is immutable and hashable, it may be used as a dictionary key or
82as a member of another set. Accordingly, it does not have methods
83like \method{add()} and \method{remove()} which could alter its contents.
84
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +000085% XXX what happens to the sets module?
Raymond Hettingered54d912003-12-31 01:59:18 +000086% The current thinking is that the sets module will be left alone.
87% That way, existing code will continue to run without alteration.
88% Also, the module provides an autoconversion feature not supported by set()
89% and frozenset().
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +000090
Raymond Hettinger7e0282f2003-11-24 07:14:54 +000091\begin{seealso}
92\seepep{218}{Adding a Built-In Set Object Type}{Originally proposed by
93Greg Wilson and ultimately implemented by Raymond Hettinger.}
94\end{seealso}
Fred Drakeed0fa3d2003-07-30 19:14:09 +000095
96%======================================================================
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +000097\section{PEP 237: Unifying Long Integers and Integers}
98
Andrew M. Kuchlingd4be86c2004-07-04 01:44:04 +000099The lengthy transition process for the PEP, begun with Python 2.2,
100takes another step forward in Python 2.4. In 2.3, certain integer
101operations that would behave differently after int/long unification
102triggered \exception{FutureWarning} warnings and returned values
103limited to 32 or 64 bits. In 2.4, these expressions no longer produce
104a warning, but they now produce a different value that's a long
105integer.
106
107The problematic expressions are primarily left shifts and lengthy
108hexadecimal and octal constants. For example, \code{2 << 32} is one
109expression that results in a warning in 2.3, evaluating to 0 on 32-bit
110platforms. In Python 2.4, this expression now returns 8589934592.
111
112
113\begin{seealso}
114\seepep{237}{Unifying Long Integers and Integers}{Original PEP
115written by Moshe Zadka and Gvr. The changes for 2.4 were implemented by
116Kalle Svensson.}
117\end{seealso}
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +0000118
119%======================================================================
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000120\section{PEP 289: Generator Expressions}
Raymond Hettinger354433a2004-05-19 08:20:33 +0000121
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000122The iterator feature introduced in Python 2.2 makes it easier to write
123programs that loop through large data sets without having the entire
124data set in memory at one time. Programmers can use iterators and the
125\module{itertools} module to write code in a fairly functional style.
126
127The fly in the ointment has been list comprehensions, because they
128produce a Python list object containing all of the items, unavoidably
129pulling them all into memory. When trying to write a program using the functional approach, it would be natural to write something like:
Raymond Hettinger354433a2004-05-19 08:20:33 +0000130
131\begin{verbatim}
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000132links = [link for link in get_all_links() if not link.followed]
133for link in links:
134 ...
Raymond Hettinger354433a2004-05-19 08:20:33 +0000135\end{verbatim}
136
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000137instead of
Raymond Hettinger354433a2004-05-19 08:20:33 +0000138
139\begin{verbatim}
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000140for link in get_all_links():
141 if link.followed:
142 continue
143 ...
144\end{verbatim}
Raymond Hettinger354433a2004-05-19 08:20:33 +0000145
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000146The first form is more concise and perhaps more readable, but if
147you're dealing with a large number of link objects the second form
148would have to be used.
Raymond Hettinger354433a2004-05-19 08:20:33 +0000149
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000150Generator expressions work similarly to list comprehensions but don't
151materialize the entire list; instead they create a generator that will
152return elements one by one. The above example could be written as:
Raymond Hettinger354433a2004-05-19 08:20:33 +0000153
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000154\begin{verbatim}
155links = (link for link in get_all_links() if not link.followed)
156for link in links:
157 ...
158\end{verbatim}
Raymond Hettinger170a6222004-05-19 19:45:19 +0000159
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000160Generator expressions always have to be written inside parentheses, as
161in the above example. The parentheses signalling a function call also
162count, so if you want to create a iterator that will be immediately
163passed to a function you could write:
Raymond Hettinger170a6222004-05-19 19:45:19 +0000164
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000165\begin{verbatim}
166print sum(obj.count for obj in list_all_objects())
167\end{verbatim}
Raymond Hettinger170a6222004-05-19 19:45:19 +0000168
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000169There are some small differences from list comprehensions. Most
170notably, the loop variable (\var{obj} in the above example) is not
171accessible outside of the generator expression. List comprehensions
172leave the variable assigned to its last value; future versions of
173Python will change this, making list comprehensions match generator
174expressions in this respect.
Raymond Hettinger354433a2004-05-19 08:20:33 +0000175
176\begin{seealso}
177\seepep{289}{Generator Expressions}{Proposed by Raymond Hettinger and
178implemented by Jiwon Seo with early efforts steered by Hye-Shik Chang.}
179\end{seealso}
180
181%======================================================================
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000182\section{PEP 322: Reverse Iteration}
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000183
Fred Drake56fcc232004-05-06 02:55:35 +0000184A new built-in function, \function{reversed(\var{seq})}, takes a sequence
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000185and returns an iterator that returns the elements of the sequence
186in reverse order.
187
188\begin{verbatim}
Raymond Hettingerbc3cba22003-11-12 16:39:30 +0000189>>> for i in reversed(xrange(1,4)):
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000190... print i
191...
1923
1932
1941
195\end{verbatim}
196
Raymond Hettingerbc3cba22003-11-12 16:39:30 +0000197Compared to extended slicing, \code{range(1,4)[::-1]}, \function{reversed()}
198is easier to read, runs faster, and uses substantially less memory.
199
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000200Note that \function{reversed()} only accepts sequences, not arbitrary
Raymond Hettingerbc3cba22003-11-12 16:39:30 +0000201iterators. If you want to reverse an iterator, first convert it to
202a list with \function{list()}.
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000203
204\begin{verbatim}
Andrew M. Kuchling44a31e12004-01-01 18:33:34 +0000205>>> input= open('/etc/passwd', 'r')
206>>> for line in reversed(list(input)):
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000207... print line
208...
209root:*:0:0:System Administrator:/var/root:/bin/tcsh
210 ...
211\end{verbatim}
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000212
Andrew M. Kuchlingf7a6b672003-11-08 16:05:37 +0000213\begin{seealso}
214\seepep{322}{Reverse Iteration}{Written and implemented by Raymond Hettinger.}
215
216\end{seealso}
217
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000218
219%======================================================================
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000220\section{PEP 327: Decimal Data Type}
221
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000222Python has always supported floating-point (FP) numbers as a data
223type, based on the underlying C \ctype{double} type. However, while
224most programming languages provide a floating-point type, most people
225(even programmers) are unaware that computing with floating-point
226numbers entails certain unavoidable inaccuracies. The new decimal
227type provides a way to avoid these inaccuracies.
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000228
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000229\subsection{Why is Decimal needed?}
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000230
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000231The limitations arise from the representation used for floating-point numbers.
232FP numbers are made up of three components:
233
234\begin{itemize}
235\item The sign, which is -1 or +1.
236\item The mantissa, which is a single-digit binary number
237followed by a fractional part. For example, \code{1.01} in base-2 notation
238is \code{1 + 0/2 + 1/4}, or 1.25 in decimal notation.
239\item The exponent, which tells where the decimal point is located in the number represented.
240\end{itemize}
241
242For example, the number 1.25 has sign +1, mantissa 1.01 (in binary),
243and exponent of 0 (the decimal point doesn't need to be shifted). The
244number 5 has the same sign and mantissa, but the exponent is 2
245because the mantissa is multiplied by 4 (2 to the power of the exponent 2).
246
247Modern systems usually provide floating-point support that conforms to
248a relevant standard called IEEE 754. C's \ctype{double} type is
249usually implemented as a 64-bit IEEE 754 number, which uses 52 bits of
250space for the mantissa. This means that numbers can only be specified
251to 52 bits of precision. If you're trying to represent numbers whose
252expansion repeats endlessly, the expansion is cut off after 52 bits.
253Unfortunately, most software needs to produce output in base 10, and
254base 10 often gives rise to such repeating decimals. For example, 1.1
255decimal is binary \code{1.0001100110011 ...}; .1 = 1/16 + 1/32 + 1/256
256plus an infinite number of additional terms. IEEE 754 has to chop off
257that infinitely repeated decimal after 52 digits, so the
258representation is slightly inaccurate.
259
260Sometimes you can see this inaccuracy when the number is printed:
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000261\begin{verbatim}
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000262>>> 1.1
2631.1000000000000001
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000264\end{verbatim}
265
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000266The inaccuracy isn't always visible when you print the number because
267the FP-to-decimal-string conversion is provided by the C library, and
268most C libraries try to produce sensible output, but the inaccuracy is
269still there and subsequent operations can magnify the error.
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000270
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000271For many applications this doesn't matter. If I'm plotting points and
272displaying them on my monitor, the difference between 1.1 and
2731.1000000000000001 is too small to be visible. Reports often limit
274output to a certain number of decimal places, and if you round the
275number to two or three or even eight decimal places, the error is
276never apparent. However, for applications where it does matter,
277it's a lot of work to implement your own custom arithmetic routines.
278
279\subsection{The \class{Decimal} type}
280
281A new module, \module{decimal}, was added to Python's standard library.
282It contains two classes, \class{Decimal} and \class{Context}.
283\class{Decimal} instances represent numbers, and
284\class{Context} instances are used to wrap up various settings such as the precision and default rounding mode.
285
286\class{Decimal} instances, like regular Python integers and FP numbers, are immutable; once they've been created, you can't change the value it represents.
287\class{Decimal} instances can be created from integers or strings:
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000288
289\begin{verbatim}
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000290>>> import decimal
291>>> decimal.Decimal(1972)
292Decimal("1972")
293>>> decimal.Decimal("1.1")
294Decimal("1.1")
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000295\end{verbatim}
296
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000297You can also provide tuples containing the sign, mantissa represented
298as a tuple of decimal digits, and exponent:
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000299
300\begin{verbatim}
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000301>>> decimal.Decimal((1, (1, 4, 7, 5), -2))
302Decimal("-14.75")
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000303\end{verbatim}
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000304
305Cautionary note: the sign bit is a Boolean value, so 0 is positive and 1 is negative.
306
307Floating-point numbers posed a bit of a problem: should the FP number
308representing 1.1 turn into the decimal number for exactly 1.1, or for
3091.1 plus whatever inaccuracies are introduced? The decision was to
310leave such a conversion out of the API. Instead, you should convert
311the floating-point number into a string using the desired precision and
312pass the string to the \class{Decimal} constructor:
313
314\begin{verbatim}
315>>> f = 1.1
316>>> decimal.Decimal(str(f))
317Decimal("1.1")
318>>> decimal.Decimal(repr(f))
319Decimal("1.1000000000000001")
320\end{verbatim}
321
322Once you have \class{Decimal} instances, you can perform the usual
323mathematical operations on them. One limitation: exponentiation
324requires an integer exponent:
325
326\begin{verbatim}
327>>> a = decimal.Decimal('35.72')
328>>> b = decimal.Decimal('1.73')
329>>> a+b
330Decimal("37.45")
331>>> a-b
332Decimal("33.99")
333>>> a*b
334Decimal("61.7956")
335>>> a/b
336Decimal("20.6473988")
337>>> a ** 2
338Decimal("1275.9184")
339>>> a ** b
340Decimal("NaN")
341\end{verbatim}
342
343You can combine \class{Decimal} instances with integers, but not with
344floating-point numbers:
345
346\begin{verbatim}
347>>> a + 4
348Decimal("39.72")
349>>> a + 4.5
350Traceback (most recent call last):
351 ...
352TypeError: You can interact Decimal only with int, long or Decimal data types.
353>>>
354\end{verbatim}
355
356\class{Decimal} numbers can be used with the \module{math} and
357\module{cmath} modules, though you'll get back a regular
358floating-point number and not a \class{Decimal}. Instances also have a \method{sqrt()} method:
359
360\begin{verbatim}
361>>> import math, cmath
362>>> d = decimal.Decimal('123456789012.345')
363>>> math.sqrt(d)
364351364.18288201344
365>>> cmath.sqrt(-d)
366351364.18288201344j
367>>> d.sqrt()
368Decimal(``351364.1828820134592177245001'')
369\end{verbatim}
370
371
372\subsection{The \class{Context} type}
373
374Instances of the \class{Context} class encapsulate several settings for
375decimal operations:
376
377\begin{itemize}
378 \item \member{prec} is the precision, the number of decimal places.
379 \item \member{rounding} specifies the rounding mode. The \module{decimal}
380 module has constants for the various possibilities:
381 \constant{ROUND_DOWN}, \constant{ROUND_CEILING}, \constant{ROUND_HALF_EVEN}, and various others.
382 \item \member{trap_enablers} is a dictionary specifying what happens on
383encountering certain error conditions: either an exception is raised or
384a value is returned. Some examples of error conditions are
385division by zero, loss of precision, and overflow.
386\end{itemize}
387
388There's a thread-local default context available by calling
389\function{getcontext()}; you can change the properties of this context
390to alter the default precision, rounding, or trap handling.
391
392\begin{verbatim}
393>>> decimal.getcontext().prec
39428
395>>> decimal.Decimal(1) / decimal.Decimal(7)
396Decimal(``0.1428571428571428571428571429'')
397>>> decimal.getcontext().prec = 9
398>>> decimal.Decimal(1) / decimal.Decimal(7)
399Decimal(``0.142857143'')
400\end{verbatim}
401
402The default action for error conditions is to return a special value
403such as infinity or not-a-number, but you can request that exceptions
404be raised:
405
406\begin{verbatim}
407>>> decimal.Decimal(1) / decimal.Decimal(0)
408Decimal(``Infinity'')
409>>> decimal.getcontext().trap_enablers[decimal.DivisionByZero] = True
410>>> decimal.Decimal(1) / decimal.Decimal(0)
411Traceback (most recent call last):
412 ...
413decimal.DivisionByZero: x / 0
414>>>
415\end{verbatim}
416
417The \class{Context} instance also has various methods for formatting
418numbers such as \method{to_eng_string()} and \method{to_sci_string()}.
419
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000420
421\begin{seealso}
422\seepep{327}{Decimal Data Type}{Written by Facundo Batista and implemented
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000423 by Facundo Batista, Eric Price, Raymond Hettinger, Aahz, and Tim Peters.}
424
425\seeurl{http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html}
426{A more detailed overview of the IEEE-754 representation.}
427
428\seeurl{http://www.lahey.com/float.htm}
429{The article uses Fortran code to illustrate many of the problems
430that floating-point inaccuracy can cause.}
431
432\seeurl{http://www2.hursley.ibm.com/decimal/}
433{A description of a decimal-based representation. This representation
434is being proposed as a standard, and underlies the new Python decimal
435type. Much of this material was written by Mike Cowlishaw, designer of the
436REXX language.}
437
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000438\end{seealso}
439
440
441%======================================================================
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000442\section{Other Language Changes}
443
444Here are all of the changes that Python 2.4 makes to the core Python
445language.
446
447\begin{itemize}
Raymond Hettingerd4462302003-11-26 17:52:45 +0000448
Raymond Hettinger31017ae2004-03-04 08:25:44 +0000449\item The \method{dict.update()} method now accepts the same
450argument forms as the \class{dict} constructor. This includes any
Andrew M. Kuchlingd0b6d9d2004-07-04 15:35:00 +0000451mapping, any iterable of key/value pairs, and keyword arguments.
Raymond Hettinger31017ae2004-03-04 08:25:44 +0000452
Raymond Hettingerd4462302003-11-26 17:52:45 +0000453\item The string methods, \method{ljust()}, \method{rjust()}, and
Andrew M. Kuchling67087562003-11-26 18:03:48 +0000454\method{center()} now take an optional argument for specifying a
Raymond Hettingerd4462302003-11-26 17:52:45 +0000455fill character other than a space.
456
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +0000457\item Strings also gained an \method{rsplit()} method that
Raymond Hettingered54d912003-12-31 01:59:18 +0000458works like the \method{split()} method but splits from the end of
Andrew M. Kuchling44a31e12004-01-01 18:33:34 +0000459the string.
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +0000460
461\begin{verbatim}
Raymond Hettinger7a6d2972004-02-13 19:00:07 +0000462>>> 'www.python.org'.split('.', 1)
463['www', 'python.org']
464'www.python.org'.rsplit('.', 1)
465['www.python', 'org']
466\end{verbatim}
Raymond Hettinger97ef8de2004-01-05 00:29:57 +0000467
Andrew M. Kuchling2fb4d512003-10-21 12:31:16 +0000468\item The \method{sort()} method of lists gained three keyword
469arguments, \var{cmp}, \var{key}, and \var{reverse}. These arguments
470make some common usages of \method{sort()} simpler. All are optional.
471
472\var{cmp} is the same as the previous single argument to
473\method{sort()}; if provided, the value should be a comparison
474function that takes two arguments and returns -1, 0, or +1 depending
475on how the arguments compare.
476
477\var{key} should be a single-argument function that takes a list
478element and returns a comparison key for the element. The list is
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000479then sorted using the comparison keys. The following example sorts a
480list case-insensitively:
Andrew M. Kuchling2fb4d512003-10-21 12:31:16 +0000481
482\begin{verbatim}
483>>> L = ['A', 'b', 'c', 'D']
484>>> L.sort() # Case-sensitive sort
485>>> L
486['A', 'D', 'b', 'c']
487>>> L.sort(key=lambda x: x.lower())
488>>> L
489['A', 'b', 'c', 'D']
490>>> L.sort(cmp=lambda x,y: cmp(x.lower(), y.lower()))
491>>> L
492['A', 'b', 'c', 'D']
493\end{verbatim}
494
495The last example, which uses the \var{cmp} parameter, is the old way
Raymond Hettingered54d912003-12-31 01:59:18 +0000496to perform a case-insensitive sort. It works but is slower than
Andrew M. Kuchling2fb4d512003-10-21 12:31:16 +0000497using a \var{key} parameter. Using \var{key} results in calling the
498\method{lower()} method once for each element in the list while using
499\var{cmp} will call the method twice for each comparison.
500
Andrew M. Kuchling981a9182003-11-13 21:33:26 +0000501For simple key functions and comparison functions, it is often
502possible to avoid a \keyword{lambda} expression by using an unbound
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000503method instead. For example, the above case-insensitive sort is best
504coded as:
505
506\begin{verbatim}
507>>> L.sort(key=str.lower)
508>>> L
509['A', 'b', 'c', 'D']
510\end{verbatim}
511
Andrew M. Kuchling2fb4d512003-10-21 12:31:16 +0000512The \var{reverse} parameter should have a Boolean value. If the value is
513\constant{True}, the list will be sorted into reverse order. Instead
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000514of \code{L.sort(lambda x,y: cmp(y.score, x.score))}, you can now write:
515\code{L.sort(key = lambda x: x.score, reverse=True)}.
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000516
Andrew M. Kuchling981a9182003-11-13 21:33:26 +0000517The results of sorting are now guaranteed to be stable. This means
518that two entries with equal keys will be returned in the same order as
519they were input. For example, you can sort a list of people by name,
520and then sort the list by age, resulting in a list sorted by age where
521people with the same age are in name-sorted order.
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000522
Fred Drake56fcc232004-05-06 02:55:35 +0000523\item There is a new built-in function
524\function{sorted(\var{iterable})} that works like the in-place
525\method{list.sort()} method but has been made suitable for use in
526expressions. The differences are:
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000527 \begin{itemize}
Raymond Hettinger7d1dd042003-11-12 16:42:10 +0000528 \item the input may be any iterable;
529 \item a newly formed copy is sorted, leaving the original intact; and
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000530 \item the expression returns the new sorted copy
531 \end{itemize}
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000532
533\begin{verbatim}
534>>> L = [9,7,8,3,2,4,1,6,5]
Raymond Hettinger64958a12003-12-17 20:43:33 +0000535>>> [10+i for i in sorted(L)] # usable in a list comprehension
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000536[11, 12, 13, 14, 15, 16, 17, 18, 19]
537>>> L = [9,7,8,3,2,4,1,6,5] # original is left unchanged
538[9,7,8,3,2,4,1,6,5]
Raymond Hettingerd4462302003-11-26 17:52:45 +0000539
Raymond Hettinger64958a12003-12-17 20:43:33 +0000540>>> sorted('Monte Python') # any iterable may be an input
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000541[' ', 'M', 'P', 'e', 'h', 'n', 'n', 'o', 'o', 't', 't', 'y']
Raymond Hettingerd4462302003-11-26 17:52:45 +0000542
543>>> # List the contents of a dict sorted by key values
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000544>>> colormap = dict(red=1, blue=2, green=3, black=4, yellow=5)
Raymond Hettinger64958a12003-12-17 20:43:33 +0000545>>> for k, v in sorted(colormap.iteritems()):
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000546... print k, v
547...
548black 4
549blue 2
550green 3
551red 1
552yellow 5
553
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000554\end{verbatim}
555
Andrew M. Kuchlingd0b6d9d2004-07-04 15:35:00 +0000556\item The \function{eval(\var{expr}, \var{globals}, \var{locals})}
557function now accepts any mapping type for the \var{locals} argument.
558Previously this had to be a regular Python dictionary.
559
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000560\item The \function{zip()} built-in function and \function{itertools.izip()}
Andrew M. Kuchling67087562003-11-26 18:03:48 +0000561 now return an empty list instead of raising a \exception{TypeError}
Andrew M. Kuchling44a31e12004-01-01 18:33:34 +0000562 exception if called with no arguments. This makes them more
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000563 suitable for use with variable length argument lists:
564
565\begin{verbatim}
566>>> def transpose(array):
567... return zip(*array)
568...
569>>> transpose([(1,2,3), (4,5,6)])
570[(1, 4), (2, 5), (3, 6)]
571>>> transpose([])
572[]
573\end{verbatim}
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000574
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000575\end{itemize}
576
577
578%======================================================================
579\subsection{Optimizations}
580
581\begin{itemize}
582
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000583\item The inner loops for \class{list} and \class{tuple} slicing
Raymond Hettingerade08ea2004-03-18 09:48:12 +0000584 were optimized and now run about one-third faster. The inner
585 loops were also optimized for \class{dict} with performance
586 boosts to \method{keys()}, \method{values()}, \method{items()},
Fred Drake9de0a2b2004-03-20 08:13:32 +0000587\method{iterkeys()}, \method{itervalues()}, and \method{iteritems()}.
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000588
Raymond Hettinger7a6d2972004-02-13 19:00:07 +0000589\item The machinery for growing and shrinking lists was optimized
Raymond Hettingerab517d22004-02-14 18:34:46 +0000590 for speed and for space efficiency. Small lists (under eight elements)
591 never over-allocate by more than three elements. Large lists do not
Raymond Hettinger7a6d2972004-02-13 19:00:07 +0000592 over-allocate by more than 1/8th. Appending and popping from lists
593 now runs faster due to more efficient code paths and less frequent
594 use of the underlying system realloc(). List comprehensions also
595 benefit. The amount of improvement varies between systems and shows
596 the greatest improvement on systems with poor realloc() implementations.
Raymond Hettinger79b5cf12004-02-17 10:46:32 +0000597 \method{list.extend()} was also optimized and no longer converts its
598 argument into a temporary list prior to extending the base list.
Raymond Hettinger7a6d2972004-02-13 19:00:07 +0000599
Raymond Hettinger97ef8de2004-01-05 00:29:57 +0000600\item \function{list()}, \function{tuple()}, \function{map()},
601 \function{filter()}, and \function{zip()} now run several times
602 faster with non-sequence arguments that supply a \method{__len__()}
603 method. Previously, the pre-sizing optimization only applied to
604 sequence arguments.
605
Raymond Hettinger23a0f4e2004-01-05 08:15:20 +0000606\item The methods \method{list.__getitem__()},
Raymond Hettinger97ef8de2004-01-05 00:29:57 +0000607 \method{dict.__getitem__()}, and \method{dict.__contains__()} are
608 are now implemented as \class{method_descriptor} objects rather
609 than \class{wrapper_descriptor} objects. This form of optimized
610 access doubles their performance and makes them more suitable for
Raymond Hettinger23a0f4e2004-01-05 08:15:20 +0000611 use as arguments to functionals:
612 \samp{map(mydict.__getitem__, keylist)}.
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000613
Fred Draked6d35d92004-06-03 13:31:22 +0000614\item Added a new opcode, \code{LIST_APPEND}, that simplifies
Raymond Hettingerdd80f762004-03-07 07:31:06 +0000615 the generated bytecode for list comprehensions and speeds them up
616 by about a third.
617
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000618\end{itemize}
619
620The net result of the 2.4 optimizations is that Python 2.4 runs the
621pystone benchmark around XX\% faster than Python 2.3 and YY\% faster
622than Python 2.2.
623
624
625%======================================================================
626\section{New, Improved, and Deprecated Modules}
627
628As usual, Python's standard library received a number of enhancements and
629bug fixes. Here's a partial list of the most notable changes, sorted
630alphabetically by module name. Consult the
631\file{Misc/NEWS} file in the source tree for a more
632complete list of changes, or look through the CVS logs for all the
633details.
634
635\begin{itemize}
636
Andrew M. Kuchlingd0b6d9d2004-07-04 15:35:00 +0000637\item The \module{asyncore} module's \function{loop()} now has a
638 \var{count} parameter that lets you perform a limited number
639 of passes through the polling loop. The default is still to loop
640 forever.
641
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +0000642\item The \module{curses} modules now supports the ncurses extension
Fred Draked6d35d92004-06-03 13:31:22 +0000643 \function{use_default_colors()}. On platforms where the terminal
644 supports transparency, this makes it possible to use a transparent
645 background. (Contributed by J\"org Lehmann.)
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000646
Raymond Hettinger0c410272004-01-05 10:13:35 +0000647\item The \module{bisect} module now has an underlying C implementation
648 for improved performance.
649 (Contributed by Dmitry Vasiliev.)
650
Andrew M. Kuchling5303a962004-01-18 15:55:51 +0000651\item The CJKCodecs collections of East Asian codecs, maintained
652by Hye-Shik Chang, was integrated into 2.4.
653The new encodings are:
654
655\begin{itemize}
656 \item Chinese (PRC): gb2312, gbk, gb18030, hz
657 \item Chinese (ROC): big5, cp950
658 \item Japanese: cp932, shift-jis, shift-jisx0213, euc-jp,
659euc-jisx0213, iso-2022-jp, iso-2022-jp-1, iso-2022-jp-2,
660 iso-2022-jp-3, iso-2022-jp-ext
661 \item Korean: cp949, euc-kr, johab, iso-2022-kr
662\end{itemize}
663
Andrew M. Kuchlingfd0e4942004-02-09 13:23:34 +0000664\item There is a new \module{collections} module for
665 various specialized collection datatypes.
666 Currently it contains just one type, \class{deque},
667 a double-ended queue that supports efficiently adding and removing
668 elements from either end.
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000669
670\begin{verbatim}
671>>> from collections import deque
672>>> d = deque('ghi') # make a new deque with three items
673>>> d.append('j') # add a new entry to the right side
674>>> d.appendleft('f') # add a new entry to the left side
675>>> d # show the representation of the deque
676deque(['f', 'g', 'h', 'i', 'j'])
677>>> d.pop() # return and remove the rightmost item
678'j'
679>>> d.popleft() # return and remove the leftmost item
680'f'
681>>> list(d) # list the contents of the deque
682['g', 'h', 'i']
683>>> 'h' in d # search the deque
684True
685\end{verbatim}
686
Andrew M. Kuchlingfd0e4942004-02-09 13:23:34 +0000687Several modules now take advantage of \class{collections.deque} for
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000688improved performance: \module{Queue}, \module{mutex}, \module{shlex}
689\module{threading}, and \module{pydoc}.
Andrew M. Kuchling5303a962004-01-18 15:55:51 +0000690
Fred Drake9f15b5c2004-05-18 04:30:00 +0000691\item The \module{ConfigParser} classes have been enhanced slightly.
692 The \method{read()} method now returns a list of the files that
693 were successfully parsed, and the \method{set()} method raises
694 \exception{TypeError} if passed a \var{value} argument that isn't a
695 string.
696
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000697\item The \module{heapq} module has been converted to C. The resulting
Andrew M. Kuchlingfd0e4942004-02-09 13:23:34 +0000698 tenfold improvement in speed makes the module suitable for handling
Raymond Hettinger33ecffb2004-06-10 05:03:17 +0000699 high volumes of data. In addition, the module has two new functions
700 \function{nlargest()} and \function{nsmallest()} that use heaps to
Andrew M. Kuchlingd0b6d9d2004-07-04 15:35:00 +0000701 find the N largest or smallest values in a dataset without the
Raymond Hettinger33ecffb2004-06-10 05:03:17 +0000702 expense of a full sort.
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000703
Andrew M. Kuchlingdff9dbd2003-11-20 22:22:19 +0000704\item The \module{imaplib} module now supports IMAP's THREAD command.
705(Contributed by Yves Dionne.)
706
Andrew M. Kuchlingad809552003-12-06 23:19:23 +0000707\item The \module{itertools} module gained a
708 \function{groupby(\var{iterable}\optional{, \var{func}})} function,
709 inspired by the GROUP BY clause from SQL.
710 \var{iterable} returns a succession of elements, and the optional
711 \var{func} is a function that takes an element and returns a key
712 value; if omitted, the key is simply the element itself.
713 \function{groupby()} then groups the elements into subsequences
714 which have matching values of the key, and returns a series of 2-tuples
715 containing the key value and an iterator over the subsequence.
716
717Here's an example. The \var{key} function simply returns whether a
718number is even or odd, so the result of \function{groupby()} is to
719return consecutive runs of odd or even numbers.
720
721\begin{verbatim}
722>>> import itertools
723>>> L = [2,4,6, 7,8,9,11, 12, 14]
724>>> for key_val, it in itertools.groupby(L, lambda x: x % 2):
725... print key_val, list(it)
726...
7270 [2, 4, 6]
7281 [7]
7290 [8]
7301 [9, 11]
7310 [12, 14]
732>>>
733\end{verbatim}
734
Raymond Hettingerfeb78c92003-12-12 13:13:47 +0000735Like its SQL counterpart, \function{groupby()} is typically used with
736sorted input. The logic for \function{groupby()} is similar to the
737\UNIX{} \code{uniq} filter which makes it handy for eliminating,
738counting, or identifying duplicate elements:
739
740\begin{verbatim}
741>>> word = 'abracadabra'
Raymond Hettingered54d912003-12-31 01:59:18 +0000742>>> letters = sorted(word) # Turn string into a sorted list of letters
Raymond Hettinger64958a12003-12-17 20:43:33 +0000743>>> letters
Andrew M. Kuchling4612bc52003-12-16 20:59:37 +0000744['a', 'a', 'a', 'a', 'a', 'b', 'b', 'c', 'd', 'r', 'r']
Raymond Hettingered54d912003-12-31 01:59:18 +0000745>>> [k for k, g in groupby(letters)] # List unique letters
Raymond Hettingerfeb78c92003-12-12 13:13:47 +0000746['a', 'b', 'c', 'd', 'r']
Raymond Hettingered54d912003-12-31 01:59:18 +0000747>>> [(k, len(list(g))) for k, g in groupby(letters)] # Count letter occurences
Raymond Hettingerfeb78c92003-12-12 13:13:47 +0000748[('a', 5), ('b', 2), ('c', 1), ('d', 1), ('r', 2)]
Raymond Hettingered54d912003-12-31 01:59:18 +0000749>>> [k for k, g in groupby(letters) if len(list(g)) > 1] # List duplicated letters
Raymond Hettingerfeb78c92003-12-12 13:13:47 +0000750['a', 'b', 'r']
751\end{verbatim}
752
Raymond Hettingered54d912003-12-31 01:59:18 +0000753\item \module{itertools} also gained a function named
754\function{tee(\var{iterator}, \var{N})} that returns \var{N} independent
755iterators that replicate \var{iterator}. If \var{N} is omitted, the
756default is 2.
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +0000757
758\begin{verbatim}
759>>> L = [1,2,3]
760>>> i1, i2 = itertools.tee(L)
761>>> i1,i2
762(<itertools.tee object at 0x402c2080>, <itertools.tee object at 0x402c2090>)
Raymond Hettingered54d912003-12-31 01:59:18 +0000763>>> list(i1) # Run the first iterator to exhaustion
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +0000764[1, 2, 3]
Raymond Hettingered54d912003-12-31 01:59:18 +0000765>>> list(i2) # Run the second iterator to exhaustion
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +0000766[1, 2, 3]
767>\end{verbatim}
768
769Note that \function{tee()} has to keep copies of the values returned
Raymond Hettingered54d912003-12-31 01:59:18 +0000770by the iterator; in the worst case, it may need to keep all of them.
Andrew M. Kuchling44a31e12004-01-01 18:33:34 +0000771This should therefore be used carefully if the leading iterator
Raymond Hettingered54d912003-12-31 01:59:18 +0000772can run far ahead of the trailing iterator in a long stream of inputs.
Andrew M. Kuchling44a31e12004-01-01 18:33:34 +0000773If the separation is large, then it becomes preferable to use
Raymond Hettingered54d912003-12-31 01:59:18 +0000774\function{list()} instead. When the iterators track closely with one
775another, \function{tee()} is ideal. Possible applications include
776bookmarking, windowing, or lookahead iterators.
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +0000777
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +0000778\item The \module{operator} module gained two new functions,
779\function{attrgetter(\var{attr})} and \function{itemgetter(\var{index})}.
780Both functions return callables that take a single argument and return
Raymond Hettingered54d912003-12-31 01:59:18 +0000781the corresponding attribute or item; these callables make excellent
782data extractors when used with \function{map()} or \function{sorted()}.
783For example:
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +0000784
785\begin{verbatim}
Raymond Hettingered54d912003-12-31 01:59:18 +0000786>>> L = [('c', 2), ('d', 1), ('a', 4), ('b', 3)]
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +0000787>>> map(operator.itemgetter(0), L)
788['c', 'd', 'a', 'b']
789>>> map(operator.itemgetter(1), L)
Raymond Hettingered54d912003-12-31 01:59:18 +0000790[2, 1, 4, 3]
791>>> sorted(L, key=operator.itemgetter(1)) # Sort list by second tuple item
792[('d', 1), ('c', 2), ('b', 3), ('a', 4)]
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +0000793\end{verbatim}
794
Andrew M. Kuchlingd0b6d9d2004-07-04 15:35:00 +0000795\item A new \function{getsid()} function was added to the
796\module{posix} module that underlies the \module{os} module.
797(Contributed by J. Raynor.)
798
799\item The \module{poplib} module now supports POP over SSL.
800
801\item The \module{profile} module can now profile C extension functions.
802% XXX more to say about this?
803
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000804\item The \module{random} module has a new method called \method{getrandbits(N)}
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000805 which returns an N-bit long integer. This method supports the existing
806 \method{randrange()} method, making it possible to efficiently generate
Andrew M. Kuchling44a31e12004-01-01 18:33:34 +0000807 arbitrarily large random numbers.
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000808
809\item The regular expression language accepted by the \module{re} module
810 was extended with simple conditional expressions, written as
811 \code{(?(\var{group})\var{A}|\var{B})}. \var{group} is either a
812 numeric group ID or a group name defined with \code{(?P<group>...)}
813 earlier in the expression. If the specified group matched, the
814 regular expression pattern \var{A} will be tested against the string; if
815 the group didn't match, the pattern \var{B} will be used instead.
Raymond Hettinger874ebd52004-05-31 03:15:02 +0000816
817\item The \module{weakref} module now supports a wider variety of objects
818 including Python functions, class instances, sets, frozensets, deques,
819 arrays, files, sockets, and regular expression pattern objects.
Andrew M. Kuchlingd0b6d9d2004-07-04 15:35:00 +0000820
821\item The \module{xmlrpclib} module now supports a multi-call extension for
822tranmitting multiple XML-RPC calls in a single HTTP operation.
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +0000823
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000824\end{itemize}
825
826
827%======================================================================
828% whole new modules get described in \subsections here
829
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000830\subsection{cookielib}
831
832The \module{cookielib} library supports client-side handling for HTTP
833cookies, just as the \module{Cookie} provides server-side cookie
834support in CGI scripts. This library manages cookies in a way similar
835to web browsers. Cookies are stored in cookie jars; the library
836transparently stores cookies offered by the web server in the cookie
837jar, and fetches the cookie from the jar when connecting to the
838server. Similar to web browsers, policy objects control whether
839cookies are accepted or not.
840
841In order to store cookies across sessions, two implementations of
842cookie jars are provided: one that stores cookies in the Netscape
843format, so applications can use the Mozilla or Lynx cookie jars, and
844one that stores cookies in the same format as the Perl libwww libary.
845
846\module{urllib2} has been changed to interact with \module{cookielib}:
847\class{HTTPCookieProcessor} manages a cookie jar that is used when
848accessing URLs.
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000849
850% ======================================================================
851\section{Build and C API Changes}
852
853Changes to Python's build process and to the C API include:
854
855\begin{itemize}
856
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000857 \item Three new convenience macros were added for common return
858 values from extension functions: \csimplemacro{Py_RETURN_NONE},
859 \csimplemacro{Py_RETURN_TRUE}, and \csimplemacro{Py_RETURN_FALSE}.
860
Fred Drakece3caf22004-02-12 18:13:12 +0000861 \item A new function, \cfunction{PyTuple_Pack(\var{N}, \var{obj1},
862 \var{obj2}, ..., \var{objN})}, constructs tuples from a variable
863 length argument list of Python objects.
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000864
Fred Drakece3caf22004-02-12 18:13:12 +0000865 \item A new function, \cfunction{PyDict_Contains(\var{d}, \var{k})},
866 implements fast dictionary lookups without masking exceptions raised
867 during the look-up process.
Raymond Hettingerd4462302003-11-26 17:52:45 +0000868
Fred Drakece3caf22004-02-12 18:13:12 +0000869 \item A new method flag, \constant{METH_COEXISTS}, allows a function
Raymond Hettinger97ef8de2004-01-05 00:29:57 +0000870 defined in slots to co-exist with a PyCFunction having the same name.
871 This can halve the access to time to a method such as
Andrew M. Kuchlingd0b6d9d2004-07-04 15:35:00 +0000872 \method{set.__contains__()}.
873
874 \item Python can now be built with additional profiling for the interpreter
875 itself, useful if you're working on the Python core.
876 Providing \longprogramopt{--enable-profiling} to the
877 \program{configure} script will let you profile the interpreter with
878 \program{gprof}, and providing the \longprogramopt{--with-tsc} switch
879 enables profiling using the Pentium's Time-Stamp-Counter.
880
881 \item The \ctype{tracebackobject} type has been renamed to \ctype{PyTracebackObject}.
Raymond Hettinger97ef8de2004-01-05 00:29:57 +0000882
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000883\end{itemize}
884
885
886%======================================================================
887\subsection{Port-Specific Changes}
888
Raymond Hettinger97ef8de2004-01-05 00:29:57 +0000889\begin{itemize}
890
891\item The Windows port now builds under MSVC++ 7.1 as well as version 6.
892
893\end{itemize}
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000894
895
896%======================================================================
897\section{Other Changes and Fixes \label{section-other}}
898
899As usual, there were a bunch of other improvements and bugfixes
900scattered throughout the source tree. A search through the CVS change
901logs finds there were XXX patches applied and YYY bugs fixed between
902Python 2.3 and 2.4. Both figures are likely to be underestimates.
903
904Some of the more notable changes are:
905
906\begin{itemize}
907
Raymond Hettinger97ef8de2004-01-05 00:29:57 +0000908\item The \module{timeit} module now automatically disables periodic
909 garbarge collection during the timing loop. This change makes
910 consecutive timings more comparable.
911
912\item The \module{base64} module now has more complete RFC 3548 support
913 for Base64, Base32, and Base16 encoding and decoding, including
914 optional case folding and optional alternative alphabets.
915 (Contributed by Barry Warsaw.)
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000916
917\end{itemize}
918
919
920%======================================================================
921\section{Porting to Python 2.4}
922
923This section lists previously described changes that may require
924changes to your code:
925
926\begin{itemize}
927
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000928\item The \function{zip()} built-in function and \function{itertools.izip()}
929 now return an empty list instead of raising a \exception{TypeError}
930 exception if called with no arguments.
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000931
932\item \function{dircache.listdir()} now passes exceptions to the caller
933 instead of returning empty lists.
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000934
Fred Drake56fcc232004-05-06 02:55:35 +0000935\item \function{LexicalHandler.startDTD()} used to receive public and
936 system ID in the wrong order. This has been corrected; applications
937 relying on the wrong order need to be fixed.
Martin v. Löwis456ab1d2004-05-06 01:54:36 +0000938
Michael W. Hudson3151e182004-06-03 13:36:42 +0000939\item \function{fcntl.ioctl} now warns if the mutate arg is omitted
Guido van Rossum6dfed6c2004-06-03 13:56:05 +0000940 and relevant.
Martin v. Löwis77ca6c42004-06-03 12:47:26 +0000941
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000942\end{itemize}
943
944
945%======================================================================
946\section{Acknowledgements \label{acks}}
947
948The author would like to thank the following people for offering
949suggestions, corrections and assistance with various drafts of this
Andrew M. Kuchling981a9182003-11-13 21:33:26 +0000950article: Raymond Hettinger.
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000951
952\end{document}