blob: d488493900d43a93cef4884b92a1173d01651e6d [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}
13\release{0.0}
14\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
Raymond Hettinger6e1fd2f2004-05-19 22:30:25 +000024This article explains the new features in Python 2.4. The release
25date is expected to be around September 2004.
Fred Drakeed0fa3d2003-07-30 19:14:09 +000026
27While Python 2.3 was primarily a library development release, Python
282.4 may extend the core language and interpreter in
29as-yet-undetermined ways.
30
31This article doesn't attempt to provide a complete specification of
32the new features, but instead provides a convenient overview. For
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +000033full details, you should refer to the documentation for Python 2.4,
34such as the \citetitle[../lib/lib.html]{Python Library Reference} and
35the \citetitle[../ref/ref.html]{Python Reference Manual}.
Fred Drakeed0fa3d2003-07-30 19:14:09 +000036If you want to understand the complete implementation and design
37rationale, refer to the PEP for a particular new feature.
38
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +000039
Raymond Hettinger7e0282f2003-11-24 07:14:54 +000040%======================================================================
41\section{PEP 218: Built-In Set Objects}
42
Fred Drake56fcc232004-05-06 02:55:35 +000043Two new built-in types, \function{set(\var{iterable})} and
44\function{frozenset(\var{iterable})} provide high speed data types for
Raymond Hettinger7e0282f2003-11-24 07:14:54 +000045membership testing, for eliminating duplicates from sequences, and
46for mathematical operations like unions, intersections, differences,
47and symmetric differences.
48
49\begin{verbatim}
50>>> a = set('abracadabra') # form a set from a string
51>>> 'z' in a # fast membership testing
52False
53>>> a # unique letters in a
54set(['a', 'r', 'b', 'c', 'd'])
55>>> ''.join(a) # convert back into a string
56'arbcd'
Raymond Hettingerd4462302003-11-26 17:52:45 +000057
Raymond Hettinger7e0282f2003-11-24 07:14:54 +000058>>> b = set('alacazam') # form a second set
59>>> a - b # letters in a but not in b
60set(['r', 'd', 'b'])
61>>> a | b # letters in either a or b
62set(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'])
63>>> a & b # letters in both a and b
64set(['a', 'c'])
65>>> a ^ b # letters in a or b but not both
66set(['r', 'd', 'b', 'm', 'z', 'l'])
Raymond Hettingerd4462302003-11-26 17:52:45 +000067
Raymond Hettinger7e0282f2003-11-24 07:14:54 +000068>>> a.add('z') # add a new element
69>>> a.update('wxy') # add multiple new elements
70>>> a
71set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'x', 'z'])
72>>> a.remove('x') # take one element out
73>>> a
74set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'z'])
75\end{verbatim}
76
77The type \function{frozenset()} is an immutable version of \function{set()}.
78Since it is immutable and hashable, it may be used as a dictionary key or
79as a member of another set. Accordingly, it does not have methods
80like \method{add()} and \method{remove()} which could alter its contents.
81
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +000082% XXX what happens to the sets module?
Raymond Hettingered54d912003-12-31 01:59:18 +000083% The current thinking is that the sets module will be left alone.
84% That way, existing code will continue to run without alteration.
85% Also, the module provides an autoconversion feature not supported by set()
86% and frozenset().
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +000087
Raymond Hettinger7e0282f2003-11-24 07:14:54 +000088\begin{seealso}
89\seepep{218}{Adding a Built-In Set Object Type}{Originally proposed by
90Greg Wilson and ultimately implemented by Raymond Hettinger.}
91\end{seealso}
Fred Drakeed0fa3d2003-07-30 19:14:09 +000092
93%======================================================================
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +000094\section{PEP 237: Unifying Long Integers and Integers}
95
Andrew M. Kuchlingd4be86c2004-07-04 01:44:04 +000096The lengthy transition process for the PEP, begun with Python 2.2,
97takes another step forward in Python 2.4. In 2.3, certain integer
98operations that would behave differently after int/long unification
99triggered \exception{FutureWarning} warnings and returned values
100limited to 32 or 64 bits. In 2.4, these expressions no longer produce
101a warning, but they now produce a different value that's a long
102integer.
103
104The problematic expressions are primarily left shifts and lengthy
105hexadecimal and octal constants. For example, \code{2 << 32} is one
106expression that results in a warning in 2.3, evaluating to 0 on 32-bit
107platforms. In Python 2.4, this expression now returns 8589934592.
108
109
110\begin{seealso}
111\seepep{237}{Unifying Long Integers and Integers}{Original PEP
112written by Moshe Zadka and Gvr. The changes for 2.4 were implemented by
113Kalle Svensson.}
114\end{seealso}
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +0000115
116%======================================================================
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000117\section{PEP 289: Generator Expressions}
Raymond Hettinger354433a2004-05-19 08:20:33 +0000118
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000119The iterator feature introduced in Python 2.2 makes it easier to write
120programs that loop through large data sets without having the entire
121data set in memory at one time. Programmers can use iterators and the
122\module{itertools} module to write code in a fairly functional style.
123
124The fly in the ointment has been list comprehensions, because they
125produce a Python list object containing all of the items, unavoidably
126pulling 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 +0000127
128\begin{verbatim}
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000129links = [link for link in get_all_links() if not link.followed]
130for link in links:
131 ...
Raymond Hettinger354433a2004-05-19 08:20:33 +0000132\end{verbatim}
133
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000134instead of
Raymond Hettinger354433a2004-05-19 08:20:33 +0000135
136\begin{verbatim}
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000137for link in get_all_links():
138 if link.followed:
139 continue
140 ...
141\end{verbatim}
Raymond Hettinger354433a2004-05-19 08:20:33 +0000142
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000143The first form is more concise and perhaps more readable, but if
144you're dealing with a large number of link objects the second form
145would have to be used.
Raymond Hettinger354433a2004-05-19 08:20:33 +0000146
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000147Generator expressions work similarly to list comprehensions but don't
148materialize the entire list; instead they create a generator that will
149return elements one by one. The above example could be written as:
Raymond Hettinger354433a2004-05-19 08:20:33 +0000150
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000151\begin{verbatim}
152links = (link for link in get_all_links() if not link.followed)
153for link in links:
154 ...
155\end{verbatim}
Raymond Hettinger170a6222004-05-19 19:45:19 +0000156
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000157Generator expressions always have to be written inside parentheses, as
158in the above example. The parentheses signalling a function call also
159count, so if you want to create a iterator that will be immediately
160passed to a function you could write:
Raymond Hettinger170a6222004-05-19 19:45:19 +0000161
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000162\begin{verbatim}
163print sum(obj.count for obj in list_all_objects())
164\end{verbatim}
Raymond Hettinger170a6222004-05-19 19:45:19 +0000165
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000166There are some small differences from list comprehensions. Most
167notably, the loop variable (\var{obj} in the above example) is not
168accessible outside of the generator expression. List comprehensions
169leave the variable assigned to its last value; future versions of
170Python will change this, making list comprehensions match generator
171expressions in this respect.
Raymond Hettinger354433a2004-05-19 08:20:33 +0000172
173\begin{seealso}
174\seepep{289}{Generator Expressions}{Proposed by Raymond Hettinger and
175implemented by Jiwon Seo with early efforts steered by Hye-Shik Chang.}
176\end{seealso}
177
178%======================================================================
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000179\section{PEP 322: Reverse Iteration}
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000180
Fred Drake56fcc232004-05-06 02:55:35 +0000181A new built-in function, \function{reversed(\var{seq})}, takes a sequence
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000182and returns an iterator that returns the elements of the sequence
183in reverse order.
184
185\begin{verbatim}
Raymond Hettingerbc3cba22003-11-12 16:39:30 +0000186>>> for i in reversed(xrange(1,4)):
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000187... print i
188...
1893
1902
1911
192\end{verbatim}
193
Raymond Hettingerbc3cba22003-11-12 16:39:30 +0000194Compared to extended slicing, \code{range(1,4)[::-1]}, \function{reversed()}
195is easier to read, runs faster, and uses substantially less memory.
196
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000197Note that \function{reversed()} only accepts sequences, not arbitrary
Raymond Hettingerbc3cba22003-11-12 16:39:30 +0000198iterators. If you want to reverse an iterator, first convert it to
199a list with \function{list()}.
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000200
201\begin{verbatim}
Andrew M. Kuchling44a31e12004-01-01 18:33:34 +0000202>>> input= open('/etc/passwd', 'r')
203>>> for line in reversed(list(input)):
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000204... print line
205...
206root:*:0:0:System Administrator:/var/root:/bin/tcsh
207 ...
208\end{verbatim}
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000209
Andrew M. Kuchlingf7a6b672003-11-08 16:05:37 +0000210\begin{seealso}
211\seepep{322}{Reverse Iteration}{Written and implemented by Raymond Hettinger.}
212
213\end{seealso}
214
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000215
216%======================================================================
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000217\section{PEP 327: Decimal Data Type}
218
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000219Python has always supported floating-point (FP) numbers as a data
220type, based on the underlying C \ctype{double} type. However, while
221most programming languages provide a floating-point type, most people
222(even programmers) are unaware that computing with floating-point
223numbers entails certain unavoidable inaccuracies. The new decimal
224type provides a way to avoid these inaccuracies.
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000225
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000226\subsection{Why is Decimal needed?}
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000227
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000228The limitations arise from the representation used for floating-point numbers.
229FP numbers are made up of three components:
230
231\begin{itemize}
232\item The sign, which is -1 or +1.
233\item The mantissa, which is a single-digit binary number
234followed by a fractional part. For example, \code{1.01} in base-2 notation
235is \code{1 + 0/2 + 1/4}, or 1.25 in decimal notation.
236\item The exponent, which tells where the decimal point is located in the number represented.
237\end{itemize}
238
239For example, the number 1.25 has sign +1, mantissa 1.01 (in binary),
240and exponent of 0 (the decimal point doesn't need to be shifted). The
241number 5 has the same sign and mantissa, but the exponent is 2
242because the mantissa is multiplied by 4 (2 to the power of the exponent 2).
243
244Modern systems usually provide floating-point support that conforms to
245a relevant standard called IEEE 754. C's \ctype{double} type is
246usually implemented as a 64-bit IEEE 754 number, which uses 52 bits of
247space for the mantissa. This means that numbers can only be specified
248to 52 bits of precision. If you're trying to represent numbers whose
249expansion repeats endlessly, the expansion is cut off after 52 bits.
250Unfortunately, most software needs to produce output in base 10, and
251base 10 often gives rise to such repeating decimals. For example, 1.1
252decimal is binary \code{1.0001100110011 ...}; .1 = 1/16 + 1/32 + 1/256
253plus an infinite number of additional terms. IEEE 754 has to chop off
254that infinitely repeated decimal after 52 digits, so the
255representation is slightly inaccurate.
256
257Sometimes you can see this inaccuracy when the number is printed:
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000258\begin{verbatim}
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000259>>> 1.1
2601.1000000000000001
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000261\end{verbatim}
262
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000263The inaccuracy isn't always visible when you print the number because
264the FP-to-decimal-string conversion is provided by the C library, and
265most C libraries try to produce sensible output, but the inaccuracy is
266still there and subsequent operations can magnify the error.
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000267
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000268For many applications this doesn't matter. If I'm plotting points and
269displaying them on my monitor, the difference between 1.1 and
2701.1000000000000001 is too small to be visible. Reports often limit
271output to a certain number of decimal places, and if you round the
272number to two or three or even eight decimal places, the error is
273never apparent. However, for applications where it does matter,
274it's a lot of work to implement your own custom arithmetic routines.
275
276\subsection{The \class{Decimal} type}
277
278A new module, \module{decimal}, was added to Python's standard library.
279It contains two classes, \class{Decimal} and \class{Context}.
280\class{Decimal} instances represent numbers, and
281\class{Context} instances are used to wrap up various settings such as the precision and default rounding mode.
282
283\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.
284\class{Decimal} instances can be created from integers or strings:
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000285
286\begin{verbatim}
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000287>>> import decimal
288>>> decimal.Decimal(1972)
289Decimal("1972")
290>>> decimal.Decimal("1.1")
291Decimal("1.1")
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000292\end{verbatim}
293
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000294You can also provide tuples containing the sign, mantissa represented
295as a tuple of decimal digits, and exponent:
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000296
297\begin{verbatim}
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000298>>> decimal.Decimal((1, (1, 4, 7, 5), -2))
299Decimal("-14.75")
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000300\end{verbatim}
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000301
302Cautionary note: the sign bit is a Boolean value, so 0 is positive and 1 is negative.
303
304Floating-point numbers posed a bit of a problem: should the FP number
305representing 1.1 turn into the decimal number for exactly 1.1, or for
3061.1 plus whatever inaccuracies are introduced? The decision was to
307leave such a conversion out of the API. Instead, you should convert
308the floating-point number into a string using the desired precision and
309pass the string to the \class{Decimal} constructor:
310
311\begin{verbatim}
312>>> f = 1.1
313>>> decimal.Decimal(str(f))
314Decimal("1.1")
315>>> decimal.Decimal(repr(f))
316Decimal("1.1000000000000001")
317\end{verbatim}
318
319Once you have \class{Decimal} instances, you can perform the usual
320mathematical operations on them. One limitation: exponentiation
321requires an integer exponent:
322
323\begin{verbatim}
324>>> a = decimal.Decimal('35.72')
325>>> b = decimal.Decimal('1.73')
326>>> a+b
327Decimal("37.45")
328>>> a-b
329Decimal("33.99")
330>>> a*b
331Decimal("61.7956")
332>>> a/b
333Decimal("20.6473988")
334>>> a ** 2
335Decimal("1275.9184")
336>>> a ** b
337Decimal("NaN")
338\end{verbatim}
339
340You can combine \class{Decimal} instances with integers, but not with
341floating-point numbers:
342
343\begin{verbatim}
344>>> a + 4
345Decimal("39.72")
346>>> a + 4.5
347Traceback (most recent call last):
348 ...
349TypeError: You can interact Decimal only with int, long or Decimal data types.
350>>>
351\end{verbatim}
352
353\class{Decimal} numbers can be used with the \module{math} and
354\module{cmath} modules, though you'll get back a regular
355floating-point number and not a \class{Decimal}. Instances also have a \method{sqrt()} method:
356
357\begin{verbatim}
358>>> import math, cmath
359>>> d = decimal.Decimal('123456789012.345')
360>>> math.sqrt(d)
361351364.18288201344
362>>> cmath.sqrt(-d)
363351364.18288201344j
364>>> d.sqrt()
365Decimal(``351364.1828820134592177245001'')
366\end{verbatim}
367
368
369\subsection{The \class{Context} type}
370
371Instances of the \class{Context} class encapsulate several settings for
372decimal operations:
373
374\begin{itemize}
375 \item \member{prec} is the precision, the number of decimal places.
376 \item \member{rounding} specifies the rounding mode. The \module{decimal}
377 module has constants for the various possibilities:
378 \constant{ROUND_DOWN}, \constant{ROUND_CEILING}, \constant{ROUND_HALF_EVEN}, and various others.
379 \item \member{trap_enablers} is a dictionary specifying what happens on
380encountering certain error conditions: either an exception is raised or
381a value is returned. Some examples of error conditions are
382division by zero, loss of precision, and overflow.
383\end{itemize}
384
385There's a thread-local default context available by calling
386\function{getcontext()}; you can change the properties of this context
387to alter the default precision, rounding, or trap handling.
388
389\begin{verbatim}
390>>> decimal.getcontext().prec
39128
392>>> decimal.Decimal(1) / decimal.Decimal(7)
393Decimal(``0.1428571428571428571428571429'')
394>>> decimal.getcontext().prec = 9
395>>> decimal.Decimal(1) / decimal.Decimal(7)
396Decimal(``0.142857143'')
397\end{verbatim}
398
399The default action for error conditions is to return a special value
400such as infinity or not-a-number, but you can request that exceptions
401be raised:
402
403\begin{verbatim}
404>>> decimal.Decimal(1) / decimal.Decimal(0)
405Decimal(``Infinity'')
406>>> decimal.getcontext().trap_enablers[decimal.DivisionByZero] = True
407>>> decimal.Decimal(1) / decimal.Decimal(0)
408Traceback (most recent call last):
409 ...
410decimal.DivisionByZero: x / 0
411>>>
412\end{verbatim}
413
414The \class{Context} instance also has various methods for formatting
415numbers such as \method{to_eng_string()} and \method{to_sci_string()}.
416
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000417
418\begin{seealso}
419\seepep{327}{Decimal Data Type}{Written by Facundo Batista and implemented
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000420 by Facundo Batista, Eric Price, Raymond Hettinger, Aahz, and Tim Peters.}
421
422\seeurl{http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html}
423{A more detailed overview of the IEEE-754 representation.}
424
425\seeurl{http://www.lahey.com/float.htm}
426{The article uses Fortran code to illustrate many of the problems
427that floating-point inaccuracy can cause.}
428
429\seeurl{http://www2.hursley.ibm.com/decimal/}
430{A description of a decimal-based representation. This representation
431is being proposed as a standard, and underlies the new Python decimal
432type. Much of this material was written by Mike Cowlishaw, designer of the
433REXX language.}
434
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000435\end{seealso}
436
437
438%======================================================================
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000439\section{Other Language Changes}
440
441Here are all of the changes that Python 2.4 makes to the core Python
442language.
443
444\begin{itemize}
Raymond Hettingerd4462302003-11-26 17:52:45 +0000445
Raymond Hettinger31017ae2004-03-04 08:25:44 +0000446\item The \method{dict.update()} method now accepts the same
447argument forms as the \class{dict} constructor. This includes any
Andrew M. Kuchlingd0b6d9d2004-07-04 15:35:00 +0000448mapping, any iterable of key/value pairs, and keyword arguments.
Raymond Hettinger31017ae2004-03-04 08:25:44 +0000449
Raymond Hettingerd4462302003-11-26 17:52:45 +0000450\item The string methods, \method{ljust()}, \method{rjust()}, and
Andrew M. Kuchling67087562003-11-26 18:03:48 +0000451\method{center()} now take an optional argument for specifying a
Raymond Hettingerd4462302003-11-26 17:52:45 +0000452fill character other than a space.
453
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +0000454\item Strings also gained an \method{rsplit()} method that
Raymond Hettingered54d912003-12-31 01:59:18 +0000455works like the \method{split()} method but splits from the end of
Andrew M. Kuchling44a31e12004-01-01 18:33:34 +0000456the string.
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +0000457
458\begin{verbatim}
Raymond Hettinger7a6d2972004-02-13 19:00:07 +0000459>>> 'www.python.org'.split('.', 1)
460['www', 'python.org']
461'www.python.org'.rsplit('.', 1)
462['www.python', 'org']
463\end{verbatim}
Raymond Hettinger97ef8de2004-01-05 00:29:57 +0000464
Andrew M. Kuchling2fb4d512003-10-21 12:31:16 +0000465\item The \method{sort()} method of lists gained three keyword
466arguments, \var{cmp}, \var{key}, and \var{reverse}. These arguments
467make some common usages of \method{sort()} simpler. All are optional.
468
469\var{cmp} is the same as the previous single argument to
470\method{sort()}; if provided, the value should be a comparison
471function that takes two arguments and returns -1, 0, or +1 depending
472on how the arguments compare.
473
474\var{key} should be a single-argument function that takes a list
475element and returns a comparison key for the element. The list is
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000476then sorted using the comparison keys. The following example sorts a
477list case-insensitively:
Andrew M. Kuchling2fb4d512003-10-21 12:31:16 +0000478
479\begin{verbatim}
480>>> L = ['A', 'b', 'c', 'D']
481>>> L.sort() # Case-sensitive sort
482>>> L
483['A', 'D', 'b', 'c']
484>>> L.sort(key=lambda x: x.lower())
485>>> L
486['A', 'b', 'c', 'D']
487>>> L.sort(cmp=lambda x,y: cmp(x.lower(), y.lower()))
488>>> L
489['A', 'b', 'c', 'D']
490\end{verbatim}
491
492The last example, which uses the \var{cmp} parameter, is the old way
Raymond Hettingered54d912003-12-31 01:59:18 +0000493to perform a case-insensitive sort. It works but is slower than
Andrew M. Kuchling2fb4d512003-10-21 12:31:16 +0000494using a \var{key} parameter. Using \var{key} results in calling the
495\method{lower()} method once for each element in the list while using
496\var{cmp} will call the method twice for each comparison.
497
Andrew M. Kuchling981a9182003-11-13 21:33:26 +0000498For simple key functions and comparison functions, it is often
499possible to avoid a \keyword{lambda} expression by using an unbound
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000500method instead. For example, the above case-insensitive sort is best
501coded as:
502
503\begin{verbatim}
504>>> L.sort(key=str.lower)
505>>> L
506['A', 'b', 'c', 'D']
507\end{verbatim}
508
Andrew M. Kuchling2fb4d512003-10-21 12:31:16 +0000509The \var{reverse} parameter should have a Boolean value. If the value is
510\constant{True}, the list will be sorted into reverse order. Instead
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000511of \code{L.sort(lambda x,y: cmp(y.score, x.score))}, you can now write:
512\code{L.sort(key = lambda x: x.score, reverse=True)}.
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000513
Andrew M. Kuchling981a9182003-11-13 21:33:26 +0000514The results of sorting are now guaranteed to be stable. This means
515that two entries with equal keys will be returned in the same order as
516they were input. For example, you can sort a list of people by name,
517and then sort the list by age, resulting in a list sorted by age where
518people with the same age are in name-sorted order.
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000519
Fred Drake56fcc232004-05-06 02:55:35 +0000520\item There is a new built-in function
521\function{sorted(\var{iterable})} that works like the in-place
522\method{list.sort()} method but has been made suitable for use in
523expressions. The differences are:
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000524 \begin{itemize}
Raymond Hettinger7d1dd042003-11-12 16:42:10 +0000525 \item the input may be any iterable;
526 \item a newly formed copy is sorted, leaving the original intact; and
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000527 \item the expression returns the new sorted copy
528 \end{itemize}
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000529
530\begin{verbatim}
531>>> L = [9,7,8,3,2,4,1,6,5]
Raymond Hettinger64958a12003-12-17 20:43:33 +0000532>>> [10+i for i in sorted(L)] # usable in a list comprehension
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000533[11, 12, 13, 14, 15, 16, 17, 18, 19]
534>>> L = [9,7,8,3,2,4,1,6,5] # original is left unchanged
535[9,7,8,3,2,4,1,6,5]
Raymond Hettingerd4462302003-11-26 17:52:45 +0000536
Raymond Hettinger64958a12003-12-17 20:43:33 +0000537>>> sorted('Monte Python') # any iterable may be an input
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000538[' ', 'M', 'P', 'e', 'h', 'n', 'n', 'o', 'o', 't', 't', 'y']
Raymond Hettingerd4462302003-11-26 17:52:45 +0000539
540>>> # List the contents of a dict sorted by key values
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000541>>> colormap = dict(red=1, blue=2, green=3, black=4, yellow=5)
Raymond Hettinger64958a12003-12-17 20:43:33 +0000542>>> for k, v in sorted(colormap.iteritems()):
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000543... print k, v
544...
545black 4
546blue 2
547green 3
548red 1
549yellow 5
550
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000551\end{verbatim}
552
Andrew M. Kuchlingd0b6d9d2004-07-04 15:35:00 +0000553\item The \function{eval(\var{expr}, \var{globals}, \var{locals})}
554function now accepts any mapping type for the \var{locals} argument.
555Previously this had to be a regular Python dictionary.
556
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000557\item The \function{zip()} built-in function and \function{itertools.izip()}
Andrew M. Kuchling67087562003-11-26 18:03:48 +0000558 now return an empty list instead of raising a \exception{TypeError}
Andrew M. Kuchling44a31e12004-01-01 18:33:34 +0000559 exception if called with no arguments. This makes them more
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000560 suitable for use with variable length argument lists:
561
562\begin{verbatim}
563>>> def transpose(array):
564... return zip(*array)
565...
566>>> transpose([(1,2,3), (4,5,6)])
567[(1, 4), (2, 5), (3, 6)]
568>>> transpose([])
569[]
570\end{verbatim}
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000571
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000572\end{itemize}
573
574
575%======================================================================
576\subsection{Optimizations}
577
578\begin{itemize}
579
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000580\item The inner loops for \class{list} and \class{tuple} slicing
Raymond Hettingerade08ea2004-03-18 09:48:12 +0000581 were optimized and now run about one-third faster. The inner
582 loops were also optimized for \class{dict} with performance
583 boosts to \method{keys()}, \method{values()}, \method{items()},
Fred Drake9de0a2b2004-03-20 08:13:32 +0000584\method{iterkeys()}, \method{itervalues()}, and \method{iteritems()}.
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000585
Raymond Hettinger7a6d2972004-02-13 19:00:07 +0000586\item The machinery for growing and shrinking lists was optimized
Raymond Hettingerab517d22004-02-14 18:34:46 +0000587 for speed and for space efficiency. Small lists (under eight elements)
588 never over-allocate by more than three elements. Large lists do not
Raymond Hettinger7a6d2972004-02-13 19:00:07 +0000589 over-allocate by more than 1/8th. Appending and popping from lists
590 now runs faster due to more efficient code paths and less frequent
591 use of the underlying system realloc(). List comprehensions also
592 benefit. The amount of improvement varies between systems and shows
593 the greatest improvement on systems with poor realloc() implementations.
Raymond Hettinger79b5cf12004-02-17 10:46:32 +0000594 \method{list.extend()} was also optimized and no longer converts its
595 argument into a temporary list prior to extending the base list.
Raymond Hettinger7a6d2972004-02-13 19:00:07 +0000596
Raymond Hettinger97ef8de2004-01-05 00:29:57 +0000597\item \function{list()}, \function{tuple()}, \function{map()},
598 \function{filter()}, and \function{zip()} now run several times
599 faster with non-sequence arguments that supply a \method{__len__()}
600 method. Previously, the pre-sizing optimization only applied to
601 sequence arguments.
602
Raymond Hettinger23a0f4e2004-01-05 08:15:20 +0000603\item The methods \method{list.__getitem__()},
Raymond Hettinger97ef8de2004-01-05 00:29:57 +0000604 \method{dict.__getitem__()}, and \method{dict.__contains__()} are
605 are now implemented as \class{method_descriptor} objects rather
606 than \class{wrapper_descriptor} objects. This form of optimized
607 access doubles their performance and makes them more suitable for
Raymond Hettinger23a0f4e2004-01-05 08:15:20 +0000608 use as arguments to functionals:
609 \samp{map(mydict.__getitem__, keylist)}.
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000610
Fred Draked6d35d92004-06-03 13:31:22 +0000611\item Added a new opcode, \code{LIST_APPEND}, that simplifies
Raymond Hettingerdd80f762004-03-07 07:31:06 +0000612 the generated bytecode for list comprehensions and speeds them up
613 by about a third.
614
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000615\end{itemize}
616
617The net result of the 2.4 optimizations is that Python 2.4 runs the
618pystone benchmark around XX\% faster than Python 2.3 and YY\% faster
619than Python 2.2.
620
621
622%======================================================================
623\section{New, Improved, and Deprecated Modules}
624
625As usual, Python's standard library received a number of enhancements and
626bug fixes. Here's a partial list of the most notable changes, sorted
627alphabetically by module name. Consult the
628\file{Misc/NEWS} file in the source tree for a more
629complete list of changes, or look through the CVS logs for all the
630details.
631
632\begin{itemize}
633
Andrew M. Kuchlingd0b6d9d2004-07-04 15:35:00 +0000634\item The \module{asyncore} module's \function{loop()} now has a
635 \var{count} parameter that lets you perform a limited number
636 of passes through the polling loop. The default is still to loop
637 forever.
638
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +0000639\item The \module{curses} modules now supports the ncurses extension
Fred Draked6d35d92004-06-03 13:31:22 +0000640 \function{use_default_colors()}. On platforms where the terminal
641 supports transparency, this makes it possible to use a transparent
642 background. (Contributed by J\"org Lehmann.)
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000643
Raymond Hettinger0c410272004-01-05 10:13:35 +0000644\item The \module{bisect} module now has an underlying C implementation
645 for improved performance.
646 (Contributed by Dmitry Vasiliev.)
647
Andrew M. Kuchling5303a962004-01-18 15:55:51 +0000648\item The CJKCodecs collections of East Asian codecs, maintained
649by Hye-Shik Chang, was integrated into 2.4.
650The new encodings are:
651
652\begin{itemize}
653 \item Chinese (PRC): gb2312, gbk, gb18030, hz
654 \item Chinese (ROC): big5, cp950
655 \item Japanese: cp932, shift-jis, shift-jisx0213, euc-jp,
656euc-jisx0213, iso-2022-jp, iso-2022-jp-1, iso-2022-jp-2,
657 iso-2022-jp-3, iso-2022-jp-ext
658 \item Korean: cp949, euc-kr, johab, iso-2022-kr
659\end{itemize}
660
Andrew M. Kuchlingfd0e4942004-02-09 13:23:34 +0000661\item There is a new \module{collections} module for
662 various specialized collection datatypes.
663 Currently it contains just one type, \class{deque},
664 a double-ended queue that supports efficiently adding and removing
665 elements from either end.
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000666
667\begin{verbatim}
668>>> from collections import deque
669>>> d = deque('ghi') # make a new deque with three items
670>>> d.append('j') # add a new entry to the right side
671>>> d.appendleft('f') # add a new entry to the left side
672>>> d # show the representation of the deque
673deque(['f', 'g', 'h', 'i', 'j'])
674>>> d.pop() # return and remove the rightmost item
675'j'
676>>> d.popleft() # return and remove the leftmost item
677'f'
678>>> list(d) # list the contents of the deque
679['g', 'h', 'i']
680>>> 'h' in d # search the deque
681True
682\end{verbatim}
683
Andrew M. Kuchlingfd0e4942004-02-09 13:23:34 +0000684Several modules now take advantage of \class{collections.deque} for
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000685improved performance: \module{Queue}, \module{mutex}, \module{shlex}
686\module{threading}, and \module{pydoc}.
Andrew M. Kuchling5303a962004-01-18 15:55:51 +0000687
Fred Drake9f15b5c2004-05-18 04:30:00 +0000688\item The \module{ConfigParser} classes have been enhanced slightly.
689 The \method{read()} method now returns a list of the files that
690 were successfully parsed, and the \method{set()} method raises
691 \exception{TypeError} if passed a \var{value} argument that isn't a
692 string.
693
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000694\item The \module{heapq} module has been converted to C. The resulting
Andrew M. Kuchlingfd0e4942004-02-09 13:23:34 +0000695 tenfold improvement in speed makes the module suitable for handling
Raymond Hettinger33ecffb2004-06-10 05:03:17 +0000696 high volumes of data. In addition, the module has two new functions
697 \function{nlargest()} and \function{nsmallest()} that use heaps to
Andrew M. Kuchlingd0b6d9d2004-07-04 15:35:00 +0000698 find the N largest or smallest values in a dataset without the
Raymond Hettinger33ecffb2004-06-10 05:03:17 +0000699 expense of a full sort.
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000700
Andrew M. Kuchlingdff9dbd2003-11-20 22:22:19 +0000701\item The \module{imaplib} module now supports IMAP's THREAD command.
702(Contributed by Yves Dionne.)
703
Andrew M. Kuchlingad809552003-12-06 23:19:23 +0000704\item The \module{itertools} module gained a
705 \function{groupby(\var{iterable}\optional{, \var{func}})} function,
706 inspired by the GROUP BY clause from SQL.
707 \var{iterable} returns a succession of elements, and the optional
708 \var{func} is a function that takes an element and returns a key
709 value; if omitted, the key is simply the element itself.
710 \function{groupby()} then groups the elements into subsequences
711 which have matching values of the key, and returns a series of 2-tuples
712 containing the key value and an iterator over the subsequence.
713
714Here's an example. The \var{key} function simply returns whether a
715number is even or odd, so the result of \function{groupby()} is to
716return consecutive runs of odd or even numbers.
717
718\begin{verbatim}
719>>> import itertools
720>>> L = [2,4,6, 7,8,9,11, 12, 14]
721>>> for key_val, it in itertools.groupby(L, lambda x: x % 2):
722... print key_val, list(it)
723...
7240 [2, 4, 6]
7251 [7]
7260 [8]
7271 [9, 11]
7280 [12, 14]
729>>>
730\end{verbatim}
731
Raymond Hettingerfeb78c92003-12-12 13:13:47 +0000732Like its SQL counterpart, \function{groupby()} is typically used with
733sorted input. The logic for \function{groupby()} is similar to the
734\UNIX{} \code{uniq} filter which makes it handy for eliminating,
735counting, or identifying duplicate elements:
736
737\begin{verbatim}
738>>> word = 'abracadabra'
Raymond Hettingered54d912003-12-31 01:59:18 +0000739>>> letters = sorted(word) # Turn string into a sorted list of letters
Raymond Hettinger64958a12003-12-17 20:43:33 +0000740>>> letters
Andrew M. Kuchling4612bc52003-12-16 20:59:37 +0000741['a', 'a', 'a', 'a', 'a', 'b', 'b', 'c', 'd', 'r', 'r']
Raymond Hettingered54d912003-12-31 01:59:18 +0000742>>> [k for k, g in groupby(letters)] # List unique letters
Raymond Hettingerfeb78c92003-12-12 13:13:47 +0000743['a', 'b', 'c', 'd', 'r']
Raymond Hettingered54d912003-12-31 01:59:18 +0000744>>> [(k, len(list(g))) for k, g in groupby(letters)] # Count letter occurences
Raymond Hettingerfeb78c92003-12-12 13:13:47 +0000745[('a', 5), ('b', 2), ('c', 1), ('d', 1), ('r', 2)]
Raymond Hettingered54d912003-12-31 01:59:18 +0000746>>> [k for k, g in groupby(letters) if len(list(g)) > 1] # List duplicated letters
Raymond Hettingerfeb78c92003-12-12 13:13:47 +0000747['a', 'b', 'r']
748\end{verbatim}
749
Raymond Hettingered54d912003-12-31 01:59:18 +0000750\item \module{itertools} also gained a function named
751\function{tee(\var{iterator}, \var{N})} that returns \var{N} independent
752iterators that replicate \var{iterator}. If \var{N} is omitted, the
753default is 2.
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +0000754
755\begin{verbatim}
756>>> L = [1,2,3]
757>>> i1, i2 = itertools.tee(L)
758>>> i1,i2
759(<itertools.tee object at 0x402c2080>, <itertools.tee object at 0x402c2090>)
Raymond Hettingered54d912003-12-31 01:59:18 +0000760>>> list(i1) # Run the first iterator to exhaustion
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +0000761[1, 2, 3]
Raymond Hettingered54d912003-12-31 01:59:18 +0000762>>> list(i2) # Run the second iterator to exhaustion
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +0000763[1, 2, 3]
764>\end{verbatim}
765
766Note that \function{tee()} has to keep copies of the values returned
Raymond Hettingered54d912003-12-31 01:59:18 +0000767by the iterator; in the worst case, it may need to keep all of them.
Andrew M. Kuchling44a31e12004-01-01 18:33:34 +0000768This should therefore be used carefully if the leading iterator
Raymond Hettingered54d912003-12-31 01:59:18 +0000769can run far ahead of the trailing iterator in a long stream of inputs.
Andrew M. Kuchling44a31e12004-01-01 18:33:34 +0000770If the separation is large, then it becomes preferable to use
Raymond Hettingered54d912003-12-31 01:59:18 +0000771\function{list()} instead. When the iterators track closely with one
772another, \function{tee()} is ideal. Possible applications include
773bookmarking, windowing, or lookahead iterators.
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +0000774
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +0000775\item The \module{operator} module gained two new functions,
776\function{attrgetter(\var{attr})} and \function{itemgetter(\var{index})}.
777Both functions return callables that take a single argument and return
Raymond Hettingered54d912003-12-31 01:59:18 +0000778the corresponding attribute or item; these callables make excellent
779data extractors when used with \function{map()} or \function{sorted()}.
780For example:
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +0000781
782\begin{verbatim}
Raymond Hettingered54d912003-12-31 01:59:18 +0000783>>> L = [('c', 2), ('d', 1), ('a', 4), ('b', 3)]
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +0000784>>> map(operator.itemgetter(0), L)
785['c', 'd', 'a', 'b']
786>>> map(operator.itemgetter(1), L)
Raymond Hettingered54d912003-12-31 01:59:18 +0000787[2, 1, 4, 3]
788>>> sorted(L, key=operator.itemgetter(1)) # Sort list by second tuple item
789[('d', 1), ('c', 2), ('b', 3), ('a', 4)]
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +0000790\end{verbatim}
791
Andrew M. Kuchlingd0b6d9d2004-07-04 15:35:00 +0000792\item A new \function{getsid()} function was added to the
793\module{posix} module that underlies the \module{os} module.
794(Contributed by J. Raynor.)
795
796\item The \module{poplib} module now supports POP over SSL.
797
798\item The \module{profile} module can now profile C extension functions.
799% XXX more to say about this?
800
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000801\item The \module{random} module has a new method called \method{getrandbits(N)}
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000802 which returns an N-bit long integer. This method supports the existing
803 \method{randrange()} method, making it possible to efficiently generate
Andrew M. Kuchling44a31e12004-01-01 18:33:34 +0000804 arbitrarily large random numbers.
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000805
806\item The regular expression language accepted by the \module{re} module
807 was extended with simple conditional expressions, written as
808 \code{(?(\var{group})\var{A}|\var{B})}. \var{group} is either a
809 numeric group ID or a group name defined with \code{(?P<group>...)}
810 earlier in the expression. If the specified group matched, the
811 regular expression pattern \var{A} will be tested against the string; if
812 the group didn't match, the pattern \var{B} will be used instead.
Raymond Hettinger874ebd52004-05-31 03:15:02 +0000813
814\item The \module{weakref} module now supports a wider variety of objects
815 including Python functions, class instances, sets, frozensets, deques,
816 arrays, files, sockets, and regular expression pattern objects.
Andrew M. Kuchlingd0b6d9d2004-07-04 15:35:00 +0000817
818\item The \module{xmlrpclib} module now supports a multi-call extension for
819tranmitting multiple XML-RPC calls in a single HTTP operation.
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +0000820
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000821\end{itemize}
822
823
824%======================================================================
825% whole new modules get described in \subsections here
826
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000827\subsection{cookielib}
828
829The \module{cookielib} library supports client-side handling for HTTP
830cookies, just as the \module{Cookie} provides server-side cookie
831support in CGI scripts. This library manages cookies in a way similar
832to web browsers. Cookies are stored in cookie jars; the library
833transparently stores cookies offered by the web server in the cookie
834jar, and fetches the cookie from the jar when connecting to the
835server. Similar to web browsers, policy objects control whether
836cookies are accepted or not.
837
838In order to store cookies across sessions, two implementations of
839cookie jars are provided: one that stores cookies in the Netscape
840format, so applications can use the Mozilla or Lynx cookie jars, and
841one that stores cookies in the same format as the Perl libwww libary.
842
843\module{urllib2} has been changed to interact with \module{cookielib}:
844\class{HTTPCookieProcessor} manages a cookie jar that is used when
845accessing URLs.
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000846
847% ======================================================================
848\section{Build and C API Changes}
849
850Changes to Python's build process and to the C API include:
851
852\begin{itemize}
853
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000854 \item Three new convenience macros were added for common return
855 values from extension functions: \csimplemacro{Py_RETURN_NONE},
856 \csimplemacro{Py_RETURN_TRUE}, and \csimplemacro{Py_RETURN_FALSE}.
857
Fred Drakece3caf22004-02-12 18:13:12 +0000858 \item A new function, \cfunction{PyTuple_Pack(\var{N}, \var{obj1},
859 \var{obj2}, ..., \var{objN})}, constructs tuples from a variable
860 length argument list of Python objects.
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000861
Fred Drakece3caf22004-02-12 18:13:12 +0000862 \item A new function, \cfunction{PyDict_Contains(\var{d}, \var{k})},
863 implements fast dictionary lookups without masking exceptions raised
864 during the look-up process.
Raymond Hettingerd4462302003-11-26 17:52:45 +0000865
Fred Drakece3caf22004-02-12 18:13:12 +0000866 \item A new method flag, \constant{METH_COEXISTS}, allows a function
Raymond Hettinger97ef8de2004-01-05 00:29:57 +0000867 defined in slots to co-exist with a PyCFunction having the same name.
868 This can halve the access to time to a method such as
Andrew M. Kuchlingd0b6d9d2004-07-04 15:35:00 +0000869 \method{set.__contains__()}.
870
871 \item Python can now be built with additional profiling for the interpreter
872 itself, useful if you're working on the Python core.
873 Providing \longprogramopt{--enable-profiling} to the
874 \program{configure} script will let you profile the interpreter with
875 \program{gprof}, and providing the \longprogramopt{--with-tsc} switch
876 enables profiling using the Pentium's Time-Stamp-Counter.
877
878 \item The \ctype{tracebackobject} type has been renamed to \ctype{PyTracebackObject}.
Raymond Hettinger97ef8de2004-01-05 00:29:57 +0000879
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000880\end{itemize}
881
882
883%======================================================================
884\subsection{Port-Specific Changes}
885
Raymond Hettinger97ef8de2004-01-05 00:29:57 +0000886\begin{itemize}
887
888\item The Windows port now builds under MSVC++ 7.1 as well as version 6.
889
890\end{itemize}
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000891
892
893%======================================================================
894\section{Other Changes and Fixes \label{section-other}}
895
896As usual, there were a bunch of other improvements and bugfixes
897scattered throughout the source tree. A search through the CVS change
898logs finds there were XXX patches applied and YYY bugs fixed between
899Python 2.3 and 2.4. Both figures are likely to be underestimates.
900
901Some of the more notable changes are:
902
903\begin{itemize}
904
Raymond Hettinger97ef8de2004-01-05 00:29:57 +0000905\item The \module{timeit} module now automatically disables periodic
906 garbarge collection during the timing loop. This change makes
907 consecutive timings more comparable.
908
909\item The \module{base64} module now has more complete RFC 3548 support
910 for Base64, Base32, and Base16 encoding and decoding, including
911 optional case folding and optional alternative alphabets.
912 (Contributed by Barry Warsaw.)
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000913
914\end{itemize}
915
916
917%======================================================================
918\section{Porting to Python 2.4}
919
920This section lists previously described changes that may require
921changes to your code:
922
923\begin{itemize}
924
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000925\item The \function{zip()} built-in function and \function{itertools.izip()}
926 now return an empty list instead of raising a \exception{TypeError}
927 exception if called with no arguments.
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +0000928
929\item \function{dircache.listdir()} now passes exceptions to the caller
930 instead of returning empty lists.
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000931
Fred Drake56fcc232004-05-06 02:55:35 +0000932\item \function{LexicalHandler.startDTD()} used to receive public and
933 system ID in the wrong order. This has been corrected; applications
934 relying on the wrong order need to be fixed.
Martin v. Löwis456ab1d2004-05-06 01:54:36 +0000935
Michael W. Hudson3151e182004-06-03 13:36:42 +0000936\item \function{fcntl.ioctl} now warns if the mutate arg is omitted
Guido van Rossum6dfed6c2004-06-03 13:56:05 +0000937 and relevant.
Martin v. Löwis77ca6c42004-06-03 12:47:26 +0000938
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000939\end{itemize}
940
941
942%======================================================================
943\section{Acknowledgements \label{acks}}
944
945The author would like to thank the following people for offering
946suggestions, corrections and assistance with various drafts of this
Andrew M. Kuchling981a9182003-11-13 21:33:26 +0000947article: Raymond Hettinger.
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000948
949\end{document}