blob: 3a12a797d0c4ef43db6e35ac81aae064fab96366 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001****************************
Georg Brandl48310cd2009-01-03 21:18:54 +00002 What's New in Python 2.4
Georg Brandl116aa622007-08-15 14:28:22 +00003****************************
4
5:Author: A.M. Kuchling
6
7.. |release| replace:: 1.02
8
Christian Heimes5b5e81c2007-12-31 16:14:33 +00009.. $Id: whatsnew24.tex 54632 2007-03-31 11:59:54Z georg.brandl $
10.. Don't write extensive text for new sections; I'll do that.
11.. Feel free to add commented-out reminders of things that need
12.. to be covered. --amk
Georg Brandl116aa622007-08-15 14:28:22 +000013
14This article explains the new features in Python 2.4.1, released on March 30,
152005.
16
17Python 2.4 is a medium-sized release. It doesn't introduce as many changes as
18the radical Python 2.2, but introduces more features than the conservative 2.3
19release. The most significant new language features are function decorators and
20generator expressions; most other changes are to the standard library.
21
22According to the CVS change logs, there were 481 patches applied and 502 bugs
23fixed between Python 2.3 and 2.4. Both figures are likely to be underestimates.
24
25This article doesn't attempt to provide a complete specification of every single
26new feature, but instead provides a brief introduction to each feature. For
27full details, you should refer to the documentation for Python 2.4, such as the
28Python Library Reference and the Python Reference Manual. Often you will be
29referred to the PEP for a particular new feature for explanations of the
30implementation and design rationale.
31
Christian Heimes5b5e81c2007-12-31 16:14:33 +000032.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +000033
34
35PEP 218: Built-In Set Objects
36=============================
37
38Python 2.3 introduced the :mod:`sets` module. C implementations of set data
39types have now been added to the Python core as two new built-in types,
40:func:`set(iterable)` and :func:`frozenset(iterable)`. They provide high speed
41operations for membership testing, for eliminating duplicates from sequences,
42and for mathematical operations like unions, intersections, differences, and
43symmetric differences. ::
44
45 >>> a = set('abracadabra') # form a set from a string
46 >>> 'z' in a # fast membership testing
47 False
48 >>> a # unique letters in a
49 set(['a', 'r', 'b', 'c', 'd'])
50 >>> ''.join(a) # convert back into a string
51 'arbcd'
52
53 >>> b = set('alacazam') # form a second set
54 >>> a - b # letters in a but not in b
55 set(['r', 'd', 'b'])
56 >>> a | b # letters in either a or b
57 set(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'])
58 >>> a & b # letters in both a and b
59 set(['a', 'c'])
60 >>> a ^ b # letters in a or b but not both
61 set(['r', 'd', 'b', 'm', 'z', 'l'])
62
63 >>> a.add('z') # add a new element
64 >>> a.update('wxy') # add multiple new elements
65 >>> a
Georg Brandl48310cd2009-01-03 21:18:54 +000066 set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'x', 'z'])
Georg Brandl116aa622007-08-15 14:28:22 +000067 >>> a.remove('x') # take one element out
68 >>> a
Georg Brandl48310cd2009-01-03 21:18:54 +000069 set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'z'])
Georg Brandl116aa622007-08-15 14:28:22 +000070
71The :func:`frozenset` type is an immutable version of :func:`set`. Since it is
72immutable and hashable, it may be used as a dictionary key or as a member of
73another set.
74
75The :mod:`sets` module remains in the standard library, and may be useful if you
76wish to subclass the :class:`Set` or :class:`ImmutableSet` classes. There are
77currently no plans to deprecate the module.
78
79
80.. seealso::
81
82 :pep:`218` - Adding a Built-In Set Object Type
83 Originally proposed by Greg Wilson and ultimately implemented by Raymond
84 Hettinger.
85
Christian Heimes5b5e81c2007-12-31 16:14:33 +000086.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +000087
88
89PEP 237: Unifying Long Integers and Integers
90============================================
91
92The lengthy transition process for this PEP, begun in Python 2.2, takes another
93step forward in Python 2.4. In 2.3, certain integer operations that would
94behave differently after int/long unification triggered :exc:`FutureWarning`
95warnings and returned values limited to 32 or 64 bits (depending on your
96platform). In 2.4, these expressions no longer produce a warning and instead
97produce a different result that's usually a long integer.
98
99The problematic expressions are primarily left shifts and lengthy hexadecimal
100and octal constants. For example, ``2 << 32`` results in a warning in 2.3,
101evaluating to 0 on 32-bit platforms. In Python 2.4, this expression now returns
102the correct answer, 8589934592.
103
104
105.. seealso::
106
107 :pep:`237` - Unifying Long Integers and Integers
108 Original PEP written by Moshe Zadka and GvR. The changes for 2.4 were
109 implemented by Kalle Svensson.
110
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000111.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000112
113
114PEP 289: Generator Expressions
115==============================
116
117The iterator feature introduced in Python 2.2 and the :mod:`itertools` module
118make it easier to write programs that loop through large data sets without
119having the entire data set in memory at one time. List comprehensions don't fit
120into this picture very well because they produce a Python list object containing
121all of the items. This unavoidably pulls all of the objects into memory, which
122can be a problem if your data set is very large. When trying to write a
123functionally-styled program, it would be natural to write something like::
124
125 links = [link for link in get_all_links() if not link.followed]
126 for link in links:
127 ...
128
129instead of ::
130
131 for link in get_all_links():
132 if link.followed:
133 continue
134 ...
135
136The first form is more concise and perhaps more readable, but if you're dealing
137with a large number of link objects you'd have to write the second form to avoid
138having all link objects in memory at the same time.
139
140Generator expressions work similarly to list comprehensions but don't
141materialize the entire list; instead they create a generator that will return
142elements one by one. The above example could be written as::
143
144 links = (link for link in get_all_links() if not link.followed)
145 for link in links:
146 ...
147
148Generator expressions always have to be written inside parentheses, as in the
149above example. The parentheses signalling a function call also count, so if you
150want to create an iterator that will be immediately passed to a function you
151could write::
152
153 print sum(obj.count for obj in list_all_objects())
154
155Generator expressions differ from list comprehensions in various small ways.
156Most notably, the loop variable (*obj* in the above example) is not accessible
157outside of the generator expression. List comprehensions leave the variable
158assigned to its last value; future versions of Python will change this, making
159list comprehensions match generator expressions in this respect.
160
161
162.. seealso::
163
164 :pep:`289` - Generator Expressions
165 Proposed by Raymond Hettinger and implemented by Jiwon Seo with early efforts
166 steered by Hye-Shik Chang.
167
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000168.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000169
170
171PEP 292: Simpler String Substitutions
172=====================================
173
174Some new classes in the standard library provide an alternative mechanism for
175substituting variables into strings; this style of substitution may be better
176for applications where untrained users need to edit templates.
177
178The usual way of substituting variables by name is the ``%`` operator::
179
180 >>> '%(page)i: %(title)s' % {'page':2, 'title': 'The Best of Times'}
181 '2: The Best of Times'
182
183When writing the template string, it can be easy to forget the ``i`` or ``s``
184after the closing parenthesis. This isn't a big problem if the template is in a
185Python module, because you run the code, get an "Unsupported format character"
186:exc:`ValueError`, and fix the problem. However, consider an application such
187as Mailman where template strings or translations are being edited by users who
188aren't aware of the Python language. The format string's syntax is complicated
189to explain to such users, and if they make a mistake, it's difficult to provide
190helpful feedback to them.
191
192PEP 292 adds a :class:`Template` class to the :mod:`string` module that uses
193``$`` to indicate a substitution::
194
195 >>> import string
196 >>> t = string.Template('$page: $title')
197 >>> t.substitute({'page':2, 'title': 'The Best of Times'})
198 '2: The Best of Times'
199
200If a key is missing from the dictionary, the :meth:`substitute` method will
201raise a :exc:`KeyError`. There's also a :meth:`safe_substitute` method that
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000202ignores missing keys::
Georg Brandl116aa622007-08-15 14:28:22 +0000203
204 >>> t = string.Template('$page: $title')
205 >>> t.safe_substitute({'page':3})
206 '3: $title'
207
Georg Brandl116aa622007-08-15 14:28:22 +0000208
209.. seealso::
210
211 :pep:`292` - Simpler String Substitutions
212 Written and implemented by Barry Warsaw.
213
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000214.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000215
216
217PEP 318: Decorators for Functions and Methods
218=============================================
219
220Python 2.2 extended Python's object model by adding static methods and class
221methods, but it didn't extend Python's syntax to provide any new way of defining
222static or class methods. Instead, you had to write a :keyword:`def` statement
223in the usual way, and pass the resulting method to a :func:`staticmethod` or
224:func:`classmethod` function that would wrap up the function as a method of the
225new type. Your code would look like this::
226
227 class C:
228 def meth (cls):
229 ...
230
231 meth = classmethod(meth) # Rebind name to wrapped-up class method
232
233If the method was very long, it would be easy to miss or forget the
234:func:`classmethod` invocation after the function body.
235
236The intention was always to add some syntax to make such definitions more
237readable, but at the time of 2.2's release a good syntax was not obvious. Today
238a good syntax *still* isn't obvious but users are asking for easier access to
239the feature; a new syntactic feature has been added to meet this need.
240
241The new feature is called "function decorators". The name comes from the idea
242that :func:`classmethod`, :func:`staticmethod`, and friends are storing
243additional information on a function object; they're *decorating* functions with
244more details.
245
246The notation borrows from Java and uses the ``'@'`` character as an indicator.
247Using the new syntax, the example above would be written::
248
249 class C:
250
251 @classmethod
252 def meth (cls):
253 ...
254
255
256The ``@classmethod`` is shorthand for the ``meth=classmethod(meth)`` assignment.
257More generally, if you have the following::
258
259 @A
260 @B
261 @C
262 def f ():
263 ...
264
265It's equivalent to the following pre-decorator code::
266
267 def f(): ...
268 f = A(B(C(f)))
269
270Decorators must come on the line before a function definition, one decorator per
271line, and can't be on the same line as the def statement, meaning that ``@A def
272f(): ...`` is illegal. You can only decorate function definitions, either at
273the module level or inside a class; you can't decorate class definitions.
274
275A decorator is just a function that takes the function to be decorated as an
276argument and returns either the same function or some new object. The return
277value of the decorator need not be callable (though it typically is), unless
278further decorators will be applied to the result. It's easy to write your own
279decorators. The following simple example just sets an attribute on the function
280object::
281
282 >>> def deco(func):
283 ... func.attr = 'decorated'
284 ... return func
285 ...
286 >>> @deco
287 ... def f(): pass
288 ...
289 >>> f
290 <function f at 0x402ef0d4>
291 >>> f.attr
292 'decorated'
293 >>>
294
295As a slightly more realistic example, the following decorator checks that the
296supplied argument is an integer::
297
298 def require_int (func):
299 def wrapper (arg):
300 assert isinstance(arg, int)
301 return func(arg)
302
303 return wrapper
304
305 @require_int
306 def p1 (arg):
307 print arg
308
309 @require_int
310 def p2(arg):
311 print arg*2
312
313An example in :pep:`318` contains a fancier version of this idea that lets you
314both specify the required type and check the returned type.
315
316Decorator functions can take arguments. If arguments are supplied, your
317decorator function is called with only those arguments and must return a new
318decorator function; this function must take a single function and return a
319function, as previously described. In other words, ``@A @B @C(args)`` becomes::
320
321 def f(): ...
322 _deco = C(args)
323 f = A(B(_deco(f)))
324
325Getting this right can be slightly brain-bending, but it's not too difficult.
326
327A small related change makes the :attr:`func_name` attribute of functions
328writable. This attribute is used to display function names in tracebacks, so
329decorators should change the name of any new function that's constructed and
330returned.
331
332
333.. seealso::
334
335 :pep:`318` - Decorators for Functions, Methods and Classes
336 Written by Kevin D. Smith, Jim Jewett, and Skip Montanaro. Several people
337 wrote patches implementing function decorators, but the one that was actually
338 checked in was patch #979728, written by Mark Russell.
339
340 http://www.python.org/moin/PythonDecoratorLibrary
341 This Wiki page contains several examples of decorators.
342
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000343.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000344
345
346PEP 322: Reverse Iteration
347==========================
348
349A new built-in function, :func:`reversed(seq)`, takes a sequence and returns an
350iterator that loops over the elements of the sequence in reverse order. ::
351
352 >>> for i in reversed(xrange(1,4)):
353 ... print i
Georg Brandl48310cd2009-01-03 21:18:54 +0000354 ...
Georg Brandl116aa622007-08-15 14:28:22 +0000355 3
356 2
357 1
358
359Compared to extended slicing, such as ``range(1,4)[::-1]``, :func:`reversed` is
360easier to read, runs faster, and uses substantially less memory.
361
362Note that :func:`reversed` only accepts sequences, not arbitrary iterators. If
363you want to reverse an iterator, first convert it to a list with :func:`list`.
364::
365
366 >>> input = open('/etc/passwd', 'r')
367 >>> for line in reversed(list(input)):
368 ... print line
Georg Brandl48310cd2009-01-03 21:18:54 +0000369 ...
Georg Brandl116aa622007-08-15 14:28:22 +0000370 root:*:0:0:System Administrator:/var/root:/bin/tcsh
371 ...
372
373
374.. seealso::
375
376 :pep:`322` - Reverse Iteration
377 Written and implemented by Raymond Hettinger.
378
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000379.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000380
381
382PEP 324: New subprocess Module
383==============================
384
385The standard library provides a number of ways to execute a subprocess, offering
386different features and different levels of complexity.
387:func:`os.system(command)` is easy to use, but slow (it runs a shell process
388which executes the command) and dangerous (you have to be careful about escaping
389the shell's metacharacters). The :mod:`popen2` module offers classes that can
390capture standard output and standard error from the subprocess, but the naming
391is confusing. The :mod:`subprocess` module cleans this up, providing a unified
392interface that offers all the features you might need.
393
394Instead of :mod:`popen2`'s collection of classes, :mod:`subprocess` contains a
395single class called :class:`Popen` whose constructor supports a number of
396different keyword arguments. ::
397
398 class Popen(args, bufsize=0, executable=None,
Georg Brandla1c6a1c2009-01-03 21:26:05 +0000399 stdin=None, stdout=None, stderr=None,
400 preexec_fn=None, close_fds=False, shell=False,
401 cwd=None, env=None, universal_newlines=False,
402 startupinfo=None, creationflags=0):
Georg Brandl116aa622007-08-15 14:28:22 +0000403
404*args* is commonly a sequence of strings that will be the arguments to the
405program executed as the subprocess. (If the *shell* argument is true, *args*
406can be a string which will then be passed on to the shell for interpretation,
407just as :func:`os.system` does.)
408
409*stdin*, *stdout*, and *stderr* specify what the subprocess's input, output, and
410error streams will be. You can provide a file object or a file descriptor, or
411you can use the constant ``subprocess.PIPE`` to create a pipe between the
412subprocess and the parent.
413
414The constructor has a number of handy options:
415
416* *close_fds* requests that all file descriptors be closed before running the
417 subprocess.
418
419* *cwd* specifies the working directory in which the subprocess will be executed
420 (defaulting to whatever the parent's working directory is).
421
422* *env* is a dictionary specifying environment variables.
423
424* *preexec_fn* is a function that gets called before the child is started.
425
426* *universal_newlines* opens the child's input and output using Python's
427 universal newline feature.
428
429Once you've created the :class:`Popen` instance, you can call its :meth:`wait`
430method to pause until the subprocess has exited, :meth:`poll` to check if it's
431exited without pausing, or :meth:`communicate(data)` to send the string *data*
432to the subprocess's standard input. :meth:`communicate(data)` then reads any
433data that the subprocess has sent to its standard output or standard error,
434returning a tuple ``(stdout_data, stderr_data)``.
435
436:func:`call` is a shortcut that passes its arguments along to the :class:`Popen`
437constructor, waits for the command to complete, and returns the status code of
438the subprocess. It can serve as a safer analog to :func:`os.system`::
439
440 sts = subprocess.call(['dpkg', '-i', '/tmp/new-package.deb'])
441 if sts == 0:
442 # Success
443 ...
444 else:
445 # dpkg returned an error
446 ...
447
448The command is invoked without use of the shell. If you really do want to use
449the shell, you can add ``shell=True`` as a keyword argument and provide a string
450instead of a sequence::
451
452 sts = subprocess.call('dpkg -i /tmp/new-package.deb', shell=True)
453
454The PEP takes various examples of shell and Python code and shows how they'd be
455translated into Python code that uses :mod:`subprocess`. Reading this section
456of the PEP is highly recommended.
457
458
459.. seealso::
460
461 :pep:`324` - subprocess - New process module
462 Written and implemented by Peter Ă…strand, with assistance from Fredrik Lundh and
463 others.
464
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000465.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000466
467
468PEP 327: Decimal Data Type
469==========================
470
471Python has always supported floating-point (FP) numbers, based on the underlying
Georg Brandl60203b42010-10-06 10:11:56 +0000472C :c:type:`double` type, as a data type. However, while most programming
Georg Brandl116aa622007-08-15 14:28:22 +0000473languages provide a floating-point type, many people (even programmers) are
474unaware that floating-point numbers don't represent certain decimal fractions
475accurately. The new :class:`Decimal` type can represent these fractions
476accurately, up to a user-specified precision limit.
477
478
479Why is Decimal needed?
480----------------------
481
482The limitations arise from the representation used for floating-point numbers.
483FP numbers are made up of three components:
484
485* The sign, which is positive or negative.
486
487* The mantissa, which is a single-digit binary number followed by a fractional
488 part. For example, ``1.01`` in base-2 notation is ``1 + 0/2 + 1/4``, or 1.25 in
489 decimal notation.
490
491* The exponent, which tells where the decimal point is located in the number
492 represented.
493
494For example, the number 1.25 has positive sign, a mantissa value of 1.01 (in
495binary), and an exponent of 0 (the decimal point doesn't need to be shifted).
496The number 5 has the same sign and mantissa, but the exponent is 2 because the
497mantissa is multiplied by 4 (2 to the power of the exponent 2); 1.25 \* 4 equals
4985.
499
500Modern systems usually provide floating-point support that conforms to a
Georg Brandl60203b42010-10-06 10:11:56 +0000501standard called IEEE 754. C's :c:type:`double` type is usually implemented as a
Georg Brandl116aa622007-08-15 14:28:22 +000050264-bit IEEE 754 number, which uses 52 bits of space for the mantissa. This
503means that numbers can only be specified to 52 bits of precision. If you're
504trying to represent numbers whose expansion repeats endlessly, the expansion is
505cut off after 52 bits. Unfortunately, most software needs to produce output in
506base 10, and common fractions in base 10 are often repeating decimals in binary.
507For example, 1.1 decimal is binary ``1.0001100110011 ...``; .1 = 1/16 + 1/32 +
5081/256 plus an infinite number of additional terms. IEEE 754 has to chop off
509that infinitely repeated decimal after 52 digits, so the representation is
510slightly inaccurate.
511
512Sometimes you can see this inaccuracy when the number is printed::
513
514 >>> 1.1
515 1.1000000000000001
516
517The inaccuracy isn't always visible when you print the number because the FP-to-
518decimal-string conversion is provided by the C library, and most C libraries try
519to produce sensible output. Even if it's not displayed, however, the inaccuracy
520is still there and subsequent operations can magnify the error.
521
522For many applications this doesn't matter. If I'm plotting points and
523displaying them on my monitor, the difference between 1.1 and 1.1000000000000001
524is too small to be visible. Reports often limit output to a certain number of
525decimal places, and if you round the number to two or three or even eight
526decimal places, the error is never apparent. However, for applications where it
527does matter, it's a lot of work to implement your own custom arithmetic
528routines.
529
530Hence, the :class:`Decimal` type was created.
531
532
533The :class:`Decimal` type
534-------------------------
535
536A new module, :mod:`decimal`, was added to Python's standard library. It
537contains two classes, :class:`Decimal` and :class:`Context`. :class:`Decimal`
538instances represent numbers, and :class:`Context` instances are used to wrap up
539various settings such as the precision and default rounding mode.
540
541:class:`Decimal` instances are immutable, like regular Python integers and FP
542numbers; once it's been created, you can't change the value an instance
543represents. :class:`Decimal` instances can be created from integers or
544strings::
545
546 >>> import decimal
547 >>> decimal.Decimal(1972)
548 Decimal("1972")
549 >>> decimal.Decimal("1.1")
550 Decimal("1.1")
551
552You can also provide tuples containing the sign, the mantissa represented as a
553tuple of decimal digits, and the exponent::
554
555 >>> decimal.Decimal((1, (1, 4, 7, 5), -2))
556 Decimal("-14.75")
557
558Cautionary note: the sign bit is a Boolean value, so 0 is positive and 1 is
559negative.
560
561Converting from floating-point numbers poses a bit of a problem: should the FP
562number representing 1.1 turn into the decimal number for exactly 1.1, or for 1.1
563plus whatever inaccuracies are introduced? The decision was to dodge the issue
564and leave such a conversion out of the API. Instead, you should convert the
565floating-point number into a string using the desired precision and pass the
566string to the :class:`Decimal` constructor::
567
568 >>> f = 1.1
569 >>> decimal.Decimal(str(f))
570 Decimal("1.1")
571 >>> decimal.Decimal('%.12f' % f)
572 Decimal("1.100000000000")
573
574Once you have :class:`Decimal` instances, you can perform the usual mathematical
575operations on them. One limitation: exponentiation requires an integer
576exponent::
577
578 >>> a = decimal.Decimal('35.72')
579 >>> b = decimal.Decimal('1.73')
580 >>> a+b
581 Decimal("37.45")
582 >>> a-b
583 Decimal("33.99")
584 >>> a*b
585 Decimal("61.7956")
586 >>> a/b
587 Decimal("20.64739884393063583815028902")
588 >>> a ** 2
589 Decimal("1275.9184")
590 >>> a**b
591 Traceback (most recent call last):
592 ...
593 decimal.InvalidOperation: x ** (non-integer)
594
595You can combine :class:`Decimal` instances with integers, but not with floating-
596point numbers::
597
598 >>> a + 4
599 Decimal("39.72")
600 >>> a + 4.5
601 Traceback (most recent call last):
602 ...
603 TypeError: You can interact Decimal only with int, long or Decimal data types.
604 >>>
605
606:class:`Decimal` numbers can be used with the :mod:`math` and :mod:`cmath`
607modules, but note that they'll be immediately converted to floating-point
608numbers before the operation is performed, resulting in a possible loss of
609precision and accuracy. You'll also get back a regular floating-point number
610and not a :class:`Decimal`. ::
611
612 >>> import math, cmath
613 >>> d = decimal.Decimal('123456789012.345')
614 >>> math.sqrt(d)
615 351364.18288201344
616 >>> cmath.sqrt(-d)
617 351364.18288201344j
618
619:class:`Decimal` instances have a :meth:`sqrt` method that returns a
620:class:`Decimal`, but if you need other things such as trigonometric functions
621you'll have to implement them. ::
622
623 >>> d.sqrt()
624 Decimal("351364.1828820134592177245001")
625
626
627The :class:`Context` type
628-------------------------
629
630Instances of the :class:`Context` class encapsulate several settings for
631decimal operations:
632
633* :attr:`prec` is the precision, the number of decimal places.
634
635* :attr:`rounding` specifies the rounding mode. The :mod:`decimal` module has
636 constants for the various possibilities: :const:`ROUND_DOWN`,
637 :const:`ROUND_CEILING`, :const:`ROUND_HALF_EVEN`, and various others.
638
639* :attr:`traps` is a dictionary specifying what happens on encountering certain
640 error conditions: either an exception is raised or a value is returned. Some
641 examples of error conditions are division by zero, loss of precision, and
642 overflow.
643
644There's a thread-local default context available by calling :func:`getcontext`;
645you can change the properties of this context to alter the default precision,
646rounding, or trap handling. The following example shows the effect of changing
647the precision of the default context::
648
649 >>> decimal.getcontext().prec
650 28
651 >>> decimal.Decimal(1) / decimal.Decimal(7)
652 Decimal("0.1428571428571428571428571429")
Georg Brandl48310cd2009-01-03 21:18:54 +0000653 >>> decimal.getcontext().prec = 9
Georg Brandl116aa622007-08-15 14:28:22 +0000654 >>> decimal.Decimal(1) / decimal.Decimal(7)
655 Decimal("0.142857143")
656
657The default action for error conditions is selectable; the module can either
658return a special value such as infinity or not-a-number, or exceptions can be
659raised::
660
661 >>> decimal.Decimal(1) / decimal.Decimal(0)
662 Traceback (most recent call last):
663 ...
664 decimal.DivisionByZero: x / 0
665 >>> decimal.getcontext().traps[decimal.DivisionByZero] = False
666 >>> decimal.Decimal(1) / decimal.Decimal(0)
667 Decimal("Infinity")
Georg Brandl48310cd2009-01-03 21:18:54 +0000668 >>>
Georg Brandl116aa622007-08-15 14:28:22 +0000669
670The :class:`Context` instance also has various methods for formatting numbers
671such as :meth:`to_eng_string` and :meth:`to_sci_string`.
672
673For more information, see the documentation for the :mod:`decimal` module, which
674includes a quick-start tutorial and a reference.
675
676
677.. seealso::
678
679 :pep:`327` - Decimal Data Type
680 Written by Facundo Batista and implemented by Facundo Batista, Eric Price,
681 Raymond Hettinger, Aahz, and Tim Peters.
682
Georg Brandl116aa622007-08-15 14:28:22 +0000683 http://www.lahey.com/float.htm
684 The article uses Fortran code to illustrate many of the problems that floating-
685 point inaccuracy can cause.
686
687 http://www2.hursley.ibm.com/decimal/
688 A description of a decimal-based representation. This representation is being
689 proposed as a standard, and underlies the new Python decimal type. Much of this
690 material was written by Mike Cowlishaw, designer of the Rexx language.
691
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000692.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000693
694
695PEP 328: Multi-line Imports
696===========================
697
698One language change is a small syntactic tweak aimed at making it easier to
699import many names from a module. In a ``from module import names`` statement,
700*names* is a sequence of names separated by commas. If the sequence is very
701long, you can either write multiple imports from the same module, or you can use
702backslashes to escape the line endings like this::
703
704 from SimpleXMLRPCServer import SimpleXMLRPCServer,\
705 SimpleXMLRPCRequestHandler,\
706 CGIXMLRPCRequestHandler,\
707 resolve_dotted_attribute
708
709The syntactic change in Python 2.4 simply allows putting the names within
710parentheses. Python ignores newlines within a parenthesized expression, so the
711backslashes are no longer needed::
712
713 from SimpleXMLRPCServer import (SimpleXMLRPCServer,
714 SimpleXMLRPCRequestHandler,
715 CGIXMLRPCRequestHandler,
716 resolve_dotted_attribute)
717
718The PEP also proposes that all :keyword:`import` statements be absolute imports,
719with a leading ``.`` character to indicate a relative import. This part of the
720PEP was not implemented for Python 2.4, but was completed for Python 2.5.
721
722
723.. seealso::
724
725 :pep:`328` - Imports: Multi-Line and Absolute/Relative
726 Written by Aahz. Multi-line imports were implemented by Dima Dorfman.
727
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000728.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000729
730
731PEP 331: Locale-Independent Float/String Conversions
732====================================================
733
734The :mod:`locale` modules lets Python software select various conversions and
735display conventions that are localized to a particular country or language.
736However, the module was careful to not change the numeric locale because various
737functions in Python's implementation required that the numeric locale remain set
738to the ``'C'`` locale. Often this was because the code was using the C
Georg Brandl60203b42010-10-06 10:11:56 +0000739library's :c:func:`atof` function.
Georg Brandl116aa622007-08-15 14:28:22 +0000740
741Not setting the numeric locale caused trouble for extensions that used third-
742party C libraries, however, because they wouldn't have the correct locale set.
743The motivating example was GTK+, whose user interface widgets weren't displaying
744numbers in the current locale.
745
746The solution described in the PEP is to add three new functions to the Python
747API that perform ASCII-only conversions, ignoring the locale setting:
748
Georg Brandl60203b42010-10-06 10:11:56 +0000749* :c:func:`PyOS_ascii_strtod(str, ptr)` and :c:func:`PyOS_ascii_atof(str, ptr)`
750 both convert a string to a C :c:type:`double`.
Georg Brandl116aa622007-08-15 14:28:22 +0000751
Georg Brandl60203b42010-10-06 10:11:56 +0000752* :c:func:`PyOS_ascii_formatd(buffer, buf_len, format, d)` converts a
753 :c:type:`double` to an ASCII string.
Georg Brandl116aa622007-08-15 14:28:22 +0000754
755The code for these functions came from the GLib library
Georg Brandl495f7b52009-10-27 15:28:25 +0000756(http://library.gnome.org/devel/glib/stable/), whose developers kindly
Georg Brandl116aa622007-08-15 14:28:22 +0000757relicensed the relevant functions and donated them to the Python Software
758Foundation. The :mod:`locale` module can now change the numeric locale,
759letting extensions such as GTK+ produce the correct results.
760
761
762.. seealso::
763
764 :pep:`331` - Locale-Independent Float/String Conversions
765 Written by Christian R. Reis, and implemented by Gustavo Carneiro.
766
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000767.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000768
769
770Other Language Changes
771======================
772
773Here are all of the changes that Python 2.4 makes to the core Python language.
774
775* Decorators for functions and methods were added (:pep:`318`).
776
777* Built-in :func:`set` and :func:`frozenset` types were added (:pep:`218`).
778 Other new built-ins include the :func:`reversed(seq)` function (:pep:`322`).
779
780* Generator expressions were added (:pep:`289`).
781
782* Certain numeric expressions no longer return values restricted to 32 or 64
783 bits (:pep:`237`).
784
785* You can now put parentheses around the list of names in a ``from module import
786 names`` statement (:pep:`328`).
787
788* The :meth:`dict.update` method now accepts the same argument forms as the
789 :class:`dict` constructor. This includes any mapping, any iterable of key/value
790 pairs, and keyword arguments. (Contributed by Raymond Hettinger.)
791
792* The string methods :meth:`ljust`, :meth:`rjust`, and :meth:`center` now take
793 an optional argument for specifying a fill character other than a space.
794 (Contributed by Raymond Hettinger.)
795
796* Strings also gained an :meth:`rsplit` method that works like the :meth:`split`
797 method but splits from the end of the string. (Contributed by Sean
798 Reifschneider.) ::
799
800 >>> 'www.python.org'.split('.', 1)
801 ['www', 'python.org']
802 'www.python.org'.rsplit('.', 1)
Georg Brandl48310cd2009-01-03 21:18:54 +0000803 ['www.python', 'org']
Georg Brandl116aa622007-08-15 14:28:22 +0000804
805* Three keyword parameters, *cmp*, *key*, and *reverse*, were added to the
806 :meth:`sort` method of lists. These parameters make some common usages of
807 :meth:`sort` simpler. All of these parameters are optional.
808
809 For the *cmp* parameter, the value should be a comparison function that takes
810 two parameters and returns -1, 0, or +1 depending on how the parameters compare.
811 This function will then be used to sort the list. Previously this was the only
812 parameter that could be provided to :meth:`sort`.
813
814 *key* should be a single-parameter function that takes a list element and
815 returns a comparison key for the element. The list is then sorted using the
816 comparison keys. The following example sorts a list case-insensitively::
817
818 >>> L = ['A', 'b', 'c', 'D']
819 >>> L.sort() # Case-sensitive sort
820 >>> L
821 ['A', 'D', 'b', 'c']
822 >>> # Using 'key' parameter to sort list
823 >>> L.sort(key=lambda x: x.lower())
824 >>> L
825 ['A', 'b', 'c', 'D']
826 >>> # Old-fashioned way
827 >>> L.sort(cmp=lambda x,y: cmp(x.lower(), y.lower()))
828 >>> L
829 ['A', 'b', 'c', 'D']
830
831 The last example, which uses the *cmp* parameter, is the old way to perform a
832 case-insensitive sort. It works but is slower than using a *key* parameter.
833 Using *key* calls :meth:`lower` method once for each element in the list while
834 using *cmp* will call it twice for each comparison, so using *key* saves on
835 invocations of the :meth:`lower` method.
836
837 For simple key functions and comparison functions, it is often possible to avoid
838 a :keyword:`lambda` expression by using an unbound method instead. For example,
839 the above case-insensitive sort is best written as::
840
841 >>> L.sort(key=str.lower)
842 >>> L
843 ['A', 'b', 'c', 'D']
844
845 Finally, the *reverse* parameter takes a Boolean value. If the value is true,
846 the list will be sorted into reverse order. Instead of ``L.sort() ;
847 L.reverse()``, you can now write ``L.sort(reverse=True)``.
848
849 The results of sorting are now guaranteed to be stable. This means that two
850 entries with equal keys will be returned in the same order as they were input.
851 For example, you can sort a list of people by name, and then sort the list by
852 age, resulting in a list sorted by age where people with the same age are in
853 name-sorted order.
854
855 (All changes to :meth:`sort` contributed by Raymond Hettinger.)
856
857* There is a new built-in function :func:`sorted(iterable)` that works like the
858 in-place :meth:`list.sort` method but can be used in expressions. The
859 differences are:
860
861* the input may be any iterable;
862
863* a newly formed copy is sorted, leaving the original intact; and
864
865* the expression returns the new sorted copy
866
867 ::
868
869 >>> L = [9,7,8,3,2,4,1,6,5]
870 >>> [10+i for i in sorted(L)] # usable in a list comprehension
871 [11, 12, 13, 14, 15, 16, 17, 18, 19]
872 >>> L # original is left unchanged
873 [9,7,8,3,2,4,1,6,5]
874 >>> sorted('Monty Python') # any iterable may be an input
875 [' ', 'M', 'P', 'h', 'n', 'n', 'o', 'o', 't', 't', 'y', 'y']
876
877 >>> # List the contents of a dict sorted by key values
878 >>> colormap = dict(red=1, blue=2, green=3, black=4, yellow=5)
879 >>> for k, v in sorted(colormap.iteritems()):
880 ... print k, v
881 ...
882 black 4
883 blue 2
884 green 3
885 red 1
886 yellow 5
887
888 (Contributed by Raymond Hettinger.)
889
890* Integer operations will no longer trigger an :exc:`OverflowWarning`. The
891 :exc:`OverflowWarning` warning will disappear in Python 2.5.
892
893* The interpreter gained a new switch, :option:`-m`, that takes a name, searches
894 for the corresponding module on ``sys.path``, and runs the module as a script.
895 For example, you can now run the Python profiler with ``python -m profile``.
896 (Contributed by Nick Coghlan.)
897
898* The :func:`eval(expr, globals, locals)` and :func:`execfile(filename, globals,
899 locals)` functions and the :keyword:`exec` statement now accept any mapping type
900 for the *locals* parameter. Previously this had to be a regular Python
901 dictionary. (Contributed by Raymond Hettinger.)
902
903* The :func:`zip` built-in function and :func:`itertools.izip` now return an
904 empty list if called with no arguments. Previously they raised a
905 :exc:`TypeError` exception. This makes them more suitable for use with variable
906 length argument lists::
907
908 >>> def transpose(array):
909 ... return zip(*array)
910 ...
911 >>> transpose([(1,2,3), (4,5,6)])
912 [(1, 4), (2, 5), (3, 6)]
913 >>> transpose([])
914 []
915
916 (Contributed by Raymond Hettinger.)
917
918* Encountering a failure while importing a module no longer leaves a partially-
919 initialized module object in ``sys.modules``. The incomplete module object left
920 behind would fool further imports of the same module into succeeding, leading to
921 confusing errors. (Fixed by Tim Peters.)
922
923* :const:`None` is now a constant; code that binds a new value to the name
924 ``None`` is now a syntax error. (Contributed by Raymond Hettinger.)
925
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000926.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000927
928
929Optimizations
930-------------
931
932* The inner loops for list and tuple slicing were optimized and now run about
933 one-third faster. The inner loops for dictionaries were also optimized,
934 resulting in performance boosts for :meth:`keys`, :meth:`values`, :meth:`items`,
935 :meth:`iterkeys`, :meth:`itervalues`, and :meth:`iteritems`. (Contributed by
936 Raymond Hettinger.)
937
938* The machinery for growing and shrinking lists was optimized for speed and for
939 space efficiency. Appending and popping from lists now runs faster due to more
940 efficient code paths and less frequent use of the underlying system
Georg Brandl60203b42010-10-06 10:11:56 +0000941 :c:func:`realloc`. List comprehensions also benefit. :meth:`list.extend` was
Georg Brandl116aa622007-08-15 14:28:22 +0000942 also optimized and no longer converts its argument into a temporary list before
943 extending the base list. (Contributed by Raymond Hettinger.)
944
945* :func:`list`, :func:`tuple`, :func:`map`, :func:`filter`, and :func:`zip` now
946 run several times faster with non-sequence arguments that supply a
947 :meth:`__len__` method. (Contributed by Raymond Hettinger.)
948
949* The methods :meth:`list.__getitem__`, :meth:`dict.__getitem__`, and
950 :meth:`dict.__contains__` are are now implemented as :class:`method_descriptor`
951 objects rather than :class:`wrapper_descriptor` objects. This form of access
952 doubles their performance and makes them more suitable for use as arguments to
953 functionals: ``map(mydict.__getitem__, keylist)``. (Contributed by Raymond
954 Hettinger.)
955
956* Added a new opcode, ``LIST_APPEND``, that simplifies the generated bytecode
957 for list comprehensions and speeds them up by about a third. (Contributed by
958 Raymond Hettinger.)
959
960* The peephole bytecode optimizer has been improved to produce shorter, faster
961 bytecode; remarkably, the resulting bytecode is more readable. (Enhanced by
962 Raymond Hettinger.)
963
964* String concatenations in statements of the form ``s = s + "abc"`` and ``s +=
965 "abc"`` are now performed more efficiently in certain circumstances. This
966 optimization won't be present in other Python implementations such as Jython, so
967 you shouldn't rely on it; using the :meth:`join` method of strings is still
968 recommended when you want to efficiently glue a large number of strings
969 together. (Contributed by Armin Rigo.)
970
971The net result of the 2.4 optimizations is that Python 2.4 runs the pystone
972benchmark around 5% faster than Python 2.3 and 35% faster than Python 2.2.
973(pystone is not a particularly good benchmark, but it's the most commonly used
974measurement of Python's performance. Your own applications may show greater or
975smaller benefits from Python 2.4.)
976
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000977.. pystone is almost useless for comparing different versions of Python;
978 instead, it excels at predicting relative Python performance on different
979 machines. So, this section would be more informative if it used other tools
980 such as pybench and parrotbench. For a more application oriented benchmark,
981 try comparing the timings of test_decimal.py under 2.3 and 2.4.
Georg Brandl116aa622007-08-15 14:28:22 +0000982
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000983.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000984
985
986New, Improved, and Deprecated Modules
987=====================================
988
989As usual, Python's standard library received a number of enhancements and bug
990fixes. Here's a partial list of the most notable changes, sorted alphabetically
991by module name. Consult the :file:`Misc/NEWS` file in the source tree for a more
992complete list of changes, or look through the CVS logs for all the details.
993
994* The :mod:`asyncore` module's :func:`loop` function now has a *count* parameter
995 that lets you perform a limited number of passes through the polling loop. The
996 default is still to loop forever.
997
998* The :mod:`base64` module now has more complete RFC 3548 support for Base64,
999 Base32, and Base16 encoding and decoding, including optional case folding and
1000 optional alternative alphabets. (Contributed by Barry Warsaw.)
1001
1002* The :mod:`bisect` module now has an underlying C implementation for improved
1003 performance. (Contributed by Dmitry Vasiliev.)
1004
1005* The CJKCodecs collections of East Asian codecs, maintained by Hye-Shik Chang,
1006 was integrated into 2.4. The new encodings are:
1007
1008* Chinese (PRC): gb2312, gbk, gb18030, big5hkscs, hz
1009
1010* Chinese (ROC): big5, cp950
1011
1012* Japanese: cp932, euc-jis-2004, euc-jp, euc-jisx0213, iso-2022-jp,
1013 iso-2022-jp-1, iso-2022-jp-2, iso-2022-jp-3, iso-2022-jp-ext, iso-2022-jp-2004,
1014 shift-jis, shift-jisx0213, shift-jis-2004
1015
1016* Korean: cp949, euc-kr, johab, iso-2022-kr
1017
1018* Some other new encodings were added: HP Roman8, ISO_8859-11, ISO_8859-16,
1019 PCTP-154, and TIS-620.
1020
1021* The UTF-8 and UTF-16 codecs now cope better with receiving partial input.
1022 Previously the :class:`StreamReader` class would try to read more data, making
1023 it impossible to resume decoding from the stream. The :meth:`read` method will
1024 now return as much data as it can and future calls will resume decoding where
1025 previous ones left off. (Implemented by Walter Dörwald.)
1026
1027* There is a new :mod:`collections` module for various specialized collection
1028 datatypes. Currently it contains just one type, :class:`deque`, a double-
1029 ended queue that supports efficiently adding and removing elements from either
1030 end::
1031
1032 >>> from collections import deque
1033 >>> d = deque('ghi') # make a new deque with three items
1034 >>> d.append('j') # add a new entry to the right side
1035 >>> d.appendleft('f') # add a new entry to the left side
1036 >>> d # show the representation of the deque
1037 deque(['f', 'g', 'h', 'i', 'j'])
1038 >>> d.pop() # return and remove the rightmost item
1039 'j'
1040 >>> d.popleft() # return and remove the leftmost item
1041 'f'
1042 >>> list(d) # list the contents of the deque
1043 ['g', 'h', 'i']
1044 >>> 'h' in d # search the deque
Georg Brandl48310cd2009-01-03 21:18:54 +00001045 True
Georg Brandl116aa622007-08-15 14:28:22 +00001046
1047 Several modules, such as the :mod:`Queue` and :mod:`threading` modules, now take
1048 advantage of :class:`collections.deque` for improved performance. (Contributed
1049 by Raymond Hettinger.)
1050
1051* The :mod:`ConfigParser` classes have been enhanced slightly. The :meth:`read`
1052 method now returns a list of the files that were successfully parsed, and the
1053 :meth:`set` method raises :exc:`TypeError` if passed a *value* argument that
1054 isn't a string. (Contributed by John Belmonte and David Goodger.)
1055
1056* The :mod:`curses` module now supports the ncurses extension
1057 :func:`use_default_colors`. On platforms where the terminal supports
1058 transparency, this makes it possible to use a transparent background.
1059 (Contributed by Jörg Lehmann.)
1060
1061* The :mod:`difflib` module now includes an :class:`HtmlDiff` class that creates
1062 an HTML table showing a side by side comparison of two versions of a text.
1063 (Contributed by Dan Gass.)
1064
1065* The :mod:`email` package was updated to version 3.0, which dropped various
1066 deprecated APIs and removes support for Python versions earlier than 2.3. The
1067 3.0 version of the package uses a new incremental parser for MIME messages,
1068 available in the :mod:`email.FeedParser` module. The new parser doesn't require
Georg Brandl7cb13192010-08-03 12:06:29 +00001069 reading the entire message into memory, and doesn't raise exceptions if a
Georg Brandl116aa622007-08-15 14:28:22 +00001070 message is malformed; instead it records any problems in the :attr:`defect`
1071 attribute of the message. (Developed by Anthony Baxter, Barry Warsaw, Thomas
1072 Wouters, and others.)
1073
1074* The :mod:`heapq` module has been converted to C. The resulting tenfold
1075 improvement in speed makes the module suitable for handling high volumes of
1076 data. In addition, the module has two new functions :func:`nlargest` and
1077 :func:`nsmallest` that use heaps to find the N largest or smallest values in a
1078 dataset without the expense of a full sort. (Contributed by Raymond Hettinger.)
1079
1080* The :mod:`httplib` module now contains constants for HTTP status codes defined
1081 in various HTTP-related RFC documents. Constants have names such as
1082 :const:`OK`, :const:`CREATED`, :const:`CONTINUE`, and
1083 :const:`MOVED_PERMANENTLY`; use pydoc to get a full list. (Contributed by
1084 Andrew Eland.)
1085
1086* The :mod:`imaplib` module now supports IMAP's THREAD command (contributed by
1087 Yves Dionne) and new :meth:`deleteacl` and :meth:`myrights` methods (contributed
1088 by Arnaud Mazin).
1089
1090* The :mod:`itertools` module gained a :func:`groupby(iterable[, *func*])`
1091 function. *iterable* is something that can be iterated over to return a stream
1092 of elements, and the optional *func* parameter is a function that takes an
1093 element and returns a key value; if omitted, the key is simply the element
1094 itself. :func:`groupby` then groups the elements into subsequences which have
1095 matching values of the key, and returns a series of 2-tuples containing the key
1096 value and an iterator over the subsequence.
1097
1098 Here's an example to make this clearer. The *key* function simply returns
1099 whether a number is even or odd, so the result of :func:`groupby` is to return
1100 consecutive runs of odd or even numbers. ::
1101
1102 >>> import itertools
1103 >>> L = [2, 4, 6, 7, 8, 9, 11, 12, 14]
1104 >>> for key_val, it in itertools.groupby(L, lambda x: x % 2):
1105 ... print key_val, list(it)
Georg Brandl48310cd2009-01-03 21:18:54 +00001106 ...
Georg Brandl116aa622007-08-15 14:28:22 +00001107 0 [2, 4, 6]
1108 1 [7]
1109 0 [8]
1110 1 [9, 11]
1111 0 [12, 14]
Georg Brandl48310cd2009-01-03 21:18:54 +00001112 >>>
Georg Brandl116aa622007-08-15 14:28:22 +00001113
1114 :func:`groupby` is typically used with sorted input. The logic for
1115 :func:`groupby` is similar to the Unix ``uniq`` filter which makes it handy for
1116 eliminating, counting, or identifying duplicate elements::
1117
1118 >>> word = 'abracadabra'
1119 >>> letters = sorted(word) # Turn string into a sorted list of letters
Georg Brandl48310cd2009-01-03 21:18:54 +00001120 >>> letters
Georg Brandl116aa622007-08-15 14:28:22 +00001121 ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'c', 'd', 'r', 'r']
1122 >>> for k, g in itertools.groupby(letters):
1123 ... print k, list(g)
Georg Brandl48310cd2009-01-03 21:18:54 +00001124 ...
Georg Brandl116aa622007-08-15 14:28:22 +00001125 a ['a', 'a', 'a', 'a', 'a']
1126 b ['b', 'b']
1127 c ['c']
1128 d ['d']
1129 r ['r', 'r']
1130 >>> # List unique letters
Georg Brandl48310cd2009-01-03 21:18:54 +00001131 >>> [k for k, g in groupby(letters)]
Georg Brandl116aa622007-08-15 14:28:22 +00001132 ['a', 'b', 'c', 'd', 'r']
1133 >>> # Count letter occurrences
Georg Brandl48310cd2009-01-03 21:18:54 +00001134 >>> [(k, len(list(g))) for k, g in groupby(letters)]
Georg Brandl116aa622007-08-15 14:28:22 +00001135 [('a', 5), ('b', 2), ('c', 1), ('d', 1), ('r', 2)]
1136
1137 (Contributed by Hye-Shik Chang.)
1138
1139* :mod:`itertools` also gained a function named :func:`tee(iterator, N)` that
1140 returns *N* independent iterators that replicate *iterator*. If *N* is omitted,
1141 the default is 2. ::
1142
1143 >>> L = [1,2,3]
1144 >>> i1, i2 = itertools.tee(L)
1145 >>> i1,i2
1146 (<itertools.tee object at 0x402c2080>, <itertools.tee object at 0x402c2090>)
1147 >>> list(i1) # Run the first iterator to exhaustion
1148 [1, 2, 3]
1149 >>> list(i2) # Run the second iterator to exhaustion
1150 [1, 2, 3]
1151
1152 Note that :func:`tee` has to keep copies of the values returned by the
1153 iterator; in the worst case, it may need to keep all of them. This should
1154 therefore be used carefully if the leading iterator can run far ahead of the
1155 trailing iterator in a long stream of inputs. If the separation is large, then
1156 you might as well use :func:`list` instead. When the iterators track closely
1157 with one another, :func:`tee` is ideal. Possible applications include
1158 bookmarking, windowing, or lookahead iterators. (Contributed by Raymond
1159 Hettinger.)
1160
1161* A number of functions were added to the :mod:`locale` module, such as
1162 :func:`bind_textdomain_codeset` to specify a particular encoding and a family of
1163 :func:`l\*gettext` functions that return messages in the chosen encoding.
1164 (Contributed by Gustavo Niemeyer.)
1165
1166* Some keyword arguments were added to the :mod:`logging` package's
1167 :func:`basicConfig` function to simplify log configuration. The default
1168 behavior is to log messages to standard error, but various keyword arguments can
1169 be specified to log to a particular file, change the logging format, or set the
1170 logging level. For example::
1171
1172 import logging
1173 logging.basicConfig(filename='/var/log/application.log',
1174 level=0, # Log all messages
Georg Brandl06788c92009-01-03 21:31:47 +00001175 format='%(levelname):%(process):%(thread):%(message)')
Georg Brandl116aa622007-08-15 14:28:22 +00001176
1177 Other additions to the :mod:`logging` package include a :meth:`log(level, msg)`
1178 convenience method, as well as a :class:`TimedRotatingFileHandler` class that
1179 rotates its log files at a timed interval. The module already had
1180 :class:`RotatingFileHandler`, which rotated logs once the file exceeded a
1181 certain size. Both classes derive from a new :class:`BaseRotatingHandler` class
1182 that can be used to implement other rotating handlers.
1183
1184 (Changes implemented by Vinay Sajip.)
1185
1186* The :mod:`marshal` module now shares interned strings on unpacking a data
1187 structure. This may shrink the size of certain pickle strings, but the primary
1188 effect is to make :file:`.pyc` files significantly smaller. (Contributed by
1189 Martin von Löwis.)
1190
1191* The :mod:`nntplib` module's :class:`NNTP` class gained :meth:`description` and
1192 :meth:`descriptions` methods to retrieve newsgroup descriptions for a single
1193 group or for a range of groups. (Contributed by JĂĽrgen A. Erhard.)
1194
1195* Two new functions were added to the :mod:`operator` module,
1196 :func:`attrgetter(attr)` and :func:`itemgetter(index)`. Both functions return
1197 callables that take a single argument and return the corresponding attribute or
1198 item; these callables make excellent data extractors when used with :func:`map`
1199 or :func:`sorted`. For example::
1200
1201 >>> L = [('c', 2), ('d', 1), ('a', 4), ('b', 3)]
1202 >>> map(operator.itemgetter(0), L)
1203 ['c', 'd', 'a', 'b']
1204 >>> map(operator.itemgetter(1), L)
1205 [2, 1, 4, 3]
1206 >>> sorted(L, key=operator.itemgetter(1)) # Sort list by second tuple item
1207 [('d', 1), ('c', 2), ('b', 3), ('a', 4)]
1208
1209 (Contributed by Raymond Hettinger.)
1210
1211* The :mod:`optparse` module was updated in various ways. The module now passes
1212 its messages through :func:`gettext.gettext`, making it possible to
1213 internationalize Optik's help and error messages. Help messages for options can
1214 now include the string ``'%default'``, which will be replaced by the option's
1215 default value. (Contributed by Greg Ward.)
1216
1217* The long-term plan is to deprecate the :mod:`rfc822` module in some future
1218 Python release in favor of the :mod:`email` package. To this end, the
1219 :func:`email.Utils.formatdate` function has been changed to make it usable as a
1220 replacement for :func:`rfc822.formatdate`. You may want to write new e-mail
1221 processing code with this in mind. (Change implemented by Anthony Baxter.)
1222
1223* A new :func:`urandom(n)` function was added to the :mod:`os` module, returning
1224 a string containing *n* bytes of random data. This function provides access to
1225 platform-specific sources of randomness such as :file:`/dev/urandom` on Linux or
1226 the Windows CryptoAPI. (Contributed by Trevor Perrin.)
1227
1228* Another new function: :func:`os.path.lexists(path)` returns true if the file
1229 specified by *path* exists, whether or not it's a symbolic link. This differs
1230 from the existing :func:`os.path.exists(path)` function, which returns false if
1231 *path* is a symlink that points to a destination that doesn't exist.
1232 (Contributed by Beni Cherniavsky.)
1233
1234* A new :func:`getsid` function was added to the :mod:`posix` module that
1235 underlies the :mod:`os` module. (Contributed by J. Raynor.)
1236
1237* The :mod:`poplib` module now supports POP over SSL. (Contributed by Hector
1238 Urtubia.)
1239
1240* The :mod:`profile` module can now profile C extension functions. (Contributed
1241 by Nick Bastin.)
1242
1243* The :mod:`random` module has a new method called :meth:`getrandbits(N)` that
1244 returns a long integer *N* bits in length. The existing :meth:`randrange`
1245 method now uses :meth:`getrandbits` where appropriate, making generation of
1246 arbitrarily large random numbers more efficient. (Contributed by Raymond
1247 Hettinger.)
1248
1249* The regular expression language accepted by the :mod:`re` module was extended
1250 with simple conditional expressions, written as ``(?(group)A|B)``. *group* is
1251 either a numeric group ID or a group name defined with ``(?P<group>...)``
1252 earlier in the expression. If the specified group matched, the regular
1253 expression pattern *A* will be tested against the string; if the group didn't
1254 match, the pattern *B* will be used instead. (Contributed by Gustavo Niemeyer.)
1255
1256* The :mod:`re` module is also no longer recursive, thanks to a massive amount
1257 of work by Gustavo Niemeyer. In a recursive regular expression engine, certain
1258 patterns result in a large amount of C stack space being consumed, and it was
1259 possible to overflow the stack. For example, if you matched a 30000-byte string
1260 of ``a`` characters against the expression ``(a|b)+``, one stack frame was
1261 consumed per character. Python 2.3 tried to check for stack overflow and raise
1262 a :exc:`RuntimeError` exception, but certain patterns could sidestep the
1263 checking and if you were unlucky Python could segfault. Python 2.4's regular
1264 expression engine can match this pattern without problems.
1265
1266* The :mod:`signal` module now performs tighter error-checking on the parameters
1267 to the :func:`signal.signal` function. For example, you can't set a handler on
1268 the :const:`SIGKILL` signal; previous versions of Python would quietly accept
1269 this, but 2.4 will raise a :exc:`RuntimeError` exception.
1270
1271* Two new functions were added to the :mod:`socket` module. :func:`socketpair`
1272 returns a pair of connected sockets and :func:`getservbyport(port)` looks up the
1273 service name for a given port number. (Contributed by Dave Cole and Barry
1274 Warsaw.)
1275
1276* The :func:`sys.exitfunc` function has been deprecated. Code should be using
1277 the existing :mod:`atexit` module, which correctly handles calling multiple exit
1278 functions. Eventually :func:`sys.exitfunc` will become a purely internal
1279 interface, accessed only by :mod:`atexit`.
1280
1281* The :mod:`tarfile` module now generates GNU-format tar files by default.
1282 (Contributed by Lars Gustaebel.)
1283
1284* The :mod:`threading` module now has an elegantly simple way to support
1285 thread-local data. The module contains a :class:`local` class whose attribute
1286 values are local to different threads. ::
1287
1288 import threading
1289
1290 data = threading.local()
1291 data.number = 42
1292 data.url = ('www.python.org', 80)
1293
1294 Other threads can assign and retrieve their own values for the :attr:`number`
1295 and :attr:`url` attributes. You can subclass :class:`local` to initialize
1296 attributes or to add methods. (Contributed by Jim Fulton.)
1297
1298* The :mod:`timeit` module now automatically disables periodic garbage
1299 collection during the timing loop. This change makes consecutive timings more
1300 comparable. (Contributed by Raymond Hettinger.)
1301
1302* The :mod:`weakref` module now supports a wider variety of objects including
1303 Python functions, class instances, sets, frozensets, deques, arrays, files,
1304 sockets, and regular expression pattern objects. (Contributed by Raymond
1305 Hettinger.)
1306
1307* The :mod:`xmlrpclib` module now supports a multi-call extension for
1308 transmitting multiple XML-RPC calls in a single HTTP operation. (Contributed by
1309 Brian Quinlan.)
1310
1311* The :mod:`mpz`, :mod:`rotor`, and :mod:`xreadlines` modules have been
1312 removed.
1313
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001314.. ======================================================================
1315.. whole new modules get described in subsections here
1316.. =====================
Georg Brandl116aa622007-08-15 14:28:22 +00001317
1318
1319cookielib
1320---------
1321
1322The :mod:`cookielib` library supports client-side handling for HTTP cookies,
1323mirroring the :mod:`Cookie` module's server-side cookie support. Cookies are
1324stored in cookie jars; the library transparently stores cookies offered by the
1325web server in the cookie jar, and fetches the cookie from the jar when
1326connecting to the server. As in web browsers, policy objects control whether
1327cookies are accepted or not.
1328
1329In order to store cookies across sessions, two implementations of cookie jars
1330are provided: one that stores cookies in the Netscape format so applications can
1331use the Mozilla or Lynx cookie files, and one that stores cookies in the same
1332format as the Perl libwww library.
1333
1334:mod:`urllib2` has been changed to interact with :mod:`cookielib`:
1335:class:`HTTPCookieProcessor` manages a cookie jar that is used when accessing
1336URLs.
1337
1338This module was contributed by John J. Lee.
1339
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001340.. ==================
Georg Brandl116aa622007-08-15 14:28:22 +00001341
1342
1343doctest
1344-------
1345
1346The :mod:`doctest` module underwent considerable refactoring thanks to Edward
1347Loper and Tim Peters. Testing can still be as simple as running
1348:func:`doctest.testmod`, but the refactorings allow customizing the module's
1349operation in various ways
1350
1351The new :class:`DocTestFinder` class extracts the tests from a given object's
1352docstrings::
1353
1354 def f (x, y):
1355 """>>> f(2,2)
1356 4
1357 >>> f(3,2)
1358 6
1359 """
1360 return x*y
1361
1362 finder = doctest.DocTestFinder()
1363
1364 # Get list of DocTest instances
1365 tests = finder.find(f)
1366
1367The new :class:`DocTestRunner` class then runs individual tests and can produce
1368a summary of the results::
1369
1370 runner = doctest.DocTestRunner()
1371 for t in tests:
1372 tried, failed = runner.run(t)
1373
1374 runner.summarize(verbose=1)
1375
1376The above example produces the following output::
1377
1378 1 items passed all tests:
1379 2 tests in f
1380 2 tests in 1 items.
1381 2 passed and 0 failed.
1382 Test passed.
1383
1384:class:`DocTestRunner` uses an instance of the :class:`OutputChecker` class to
1385compare the expected output with the actual output. This class takes a number
1386of different flags that customize its behaviour; ambitious users can also write
1387a completely new subclass of :class:`OutputChecker`.
1388
1389The default output checker provides a number of handy features. For example,
1390with the :const:`doctest.ELLIPSIS` option flag, an ellipsis (``...``) in the
1391expected output matches any substring, making it easier to accommodate outputs
1392that vary in minor ways::
1393
1394 def o (n):
1395 """>>> o(1)
1396 <__main__.C instance at 0x...>
1397 >>>
1398 """
1399
1400Another special string, ``<BLANKLINE>``, matches a blank line::
1401
1402 def p (n):
1403 """>>> p(1)
1404 <BLANKLINE>
1405 >>>
1406 """
1407
1408Another new capability is producing a diff-style display of the output by
1409specifying the :const:`doctest.REPORT_UDIFF` (unified diffs),
1410:const:`doctest.REPORT_CDIFF` (context diffs), or :const:`doctest.REPORT_NDIFF`
1411(delta-style) option flags. For example::
1412
1413 def g (n):
1414 """>>> g(4)
1415 here
1416 is
1417 a
1418 lengthy
1419 >>>"""
1420 L = 'here is a rather lengthy list of words'.split()
1421 for word in L[:n]:
1422 print word
1423
1424Running the above function's tests with :const:`doctest.REPORT_UDIFF` specified,
1425you get the following output::
1426
1427 **********************************************************************
Georg Brandl1f01deb2009-01-03 22:47:39 +00001428 File "t.py", line 15, in g
Georg Brandl116aa622007-08-15 14:28:22 +00001429 Failed example:
1430 g(4)
1431 Differences (unified diff with -expected +actual):
1432 @@ -2,3 +2,3 @@
1433 is
1434 a
1435 -lengthy
1436 +rather
1437 **********************************************************************
1438
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001439.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +00001440
1441
1442Build and C API Changes
1443=======================
1444
1445Some of the changes to Python's build process and to the C API are:
1446
1447* Three new convenience macros were added for common return values from
Georg Brandl60203b42010-10-06 10:11:56 +00001448 extension functions: :c:macro:`Py_RETURN_NONE`, :c:macro:`Py_RETURN_TRUE`, and
1449 :c:macro:`Py_RETURN_FALSE`. (Contributed by Brett Cannon.)
Georg Brandl116aa622007-08-15 14:28:22 +00001450
Georg Brandl60203b42010-10-06 10:11:56 +00001451* Another new macro, :c:macro:`Py_CLEAR(obj)`, decreases the reference count of
Georg Brandl116aa622007-08-15 14:28:22 +00001452 *obj* and sets *obj* to the null pointer. (Contributed by Jim Fulton.)
1453
Georg Brandl60203b42010-10-06 10:11:56 +00001454* A new function, :c:func:`PyTuple_Pack(N, obj1, obj2, ..., objN)`, constructs
Georg Brandl116aa622007-08-15 14:28:22 +00001455 tuples from a variable length argument list of Python objects. (Contributed by
1456 Raymond Hettinger.)
1457
Georg Brandl60203b42010-10-06 10:11:56 +00001458* A new function, :c:func:`PyDict_Contains(d, k)`, implements fast dictionary
Georg Brandl116aa622007-08-15 14:28:22 +00001459 lookups without masking exceptions raised during the look-up process.
1460 (Contributed by Raymond Hettinger.)
1461
Georg Brandl60203b42010-10-06 10:11:56 +00001462* The :c:macro:`Py_IS_NAN(X)` macro returns 1 if its float or double argument
Georg Brandl116aa622007-08-15 14:28:22 +00001463 *X* is a NaN. (Contributed by Tim Peters.)
1464
1465* C code can avoid unnecessary locking by using the new
Georg Brandl60203b42010-10-06 10:11:56 +00001466 :c:func:`PyEval_ThreadsInitialized` function to tell if any thread operations
Georg Brandl116aa622007-08-15 14:28:22 +00001467 have been performed. If this function returns false, no lock operations are
1468 needed. (Contributed by Nick Coghlan.)
1469
Georg Brandl60203b42010-10-06 10:11:56 +00001470* A new function, :c:func:`PyArg_VaParseTupleAndKeywords`, is the same as
1471 :c:func:`PyArg_ParseTupleAndKeywords` but takes a :c:type:`va_list` instead of a
Georg Brandl116aa622007-08-15 14:28:22 +00001472 number of arguments. (Contributed by Greg Chapman.)
1473
1474* A new method flag, :const:`METH_COEXISTS`, allows a function defined in slots
Georg Brandl60203b42010-10-06 10:11:56 +00001475 to co-exist with a :c:type:`PyCFunction` having the same name. This can halve
Georg Brandl116aa622007-08-15 14:28:22 +00001476 the access time for a method such as :meth:`set.__contains__`. (Contributed by
1477 Raymond Hettinger.)
1478
1479* Python can now be built with additional profiling for the interpreter itself,
1480 intended as an aid to people developing the Python core. Providing
1481 :option:`----enable-profiling` to the :program:`configure` script will let you
1482 profile the interpreter with :program:`gprof`, and providing the
1483 :option:`----with-tsc` switch enables profiling using the Pentium's Time-Stamp-
1484 Counter register. Note that the :option:`----with-tsc` switch is slightly
1485 misnamed, because the profiling feature also works on the PowerPC platform,
1486 though that processor architecture doesn't call that register "the TSC
1487 register". (Contributed by Jeremy Hylton.)
1488
Georg Brandl60203b42010-10-06 10:11:56 +00001489* The :c:type:`tracebackobject` type has been renamed to
1490 :c:type:`PyTracebackObject`.
Georg Brandl116aa622007-08-15 14:28:22 +00001491
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001492.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +00001493
1494
1495Port-Specific Changes
1496---------------------
1497
1498* The Windows port now builds under MSVC++ 7.1 as well as version 6.
1499 (Contributed by Martin von Löwis.)
1500
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001501.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +00001502
1503
1504Porting to Python 2.4
1505=====================
1506
1507This section lists previously described changes that may require changes to your
1508code:
1509
1510* Left shifts and hexadecimal/octal constants that are too large no longer
1511 trigger a :exc:`FutureWarning` and return a value limited to 32 or 64 bits;
1512 instead they return a long integer.
1513
1514* Integer operations will no longer trigger an :exc:`OverflowWarning`. The
1515 :exc:`OverflowWarning` warning will disappear in Python 2.5.
1516
1517* The :func:`zip` built-in function and :func:`itertools.izip` now return an
1518 empty list instead of raising a :exc:`TypeError` exception if called with no
1519 arguments.
1520
1521* You can no longer compare the :class:`date` and :class:`datetime` instances
1522 provided by the :mod:`datetime` module. Two instances of different classes
1523 will now always be unequal, and relative comparisons (``<``, ``>``) will raise
1524 a :exc:`TypeError`.
1525
1526* :func:`dircache.listdir` now passes exceptions to the caller instead of
1527 returning empty lists.
1528
1529* :func:`LexicalHandler.startDTD` used to receive the public and system IDs in
1530 the wrong order. This has been corrected; applications relying on the wrong
1531 order need to be fixed.
1532
1533* :func:`fcntl.ioctl` now warns if the *mutate* argument is omitted and
1534 relevant.
1535
1536* The :mod:`tarfile` module now generates GNU-format tar files by default.
1537
1538* Encountering a failure while importing a module no longer leaves a partially-
1539 initialized module object in ``sys.modules``.
1540
1541* :const:`None` is now a constant; code that binds a new value to the name
1542 ``None`` is now a syntax error.
1543
1544* The :func:`signals.signal` function now raises a :exc:`RuntimeError` exception
1545 for certain illegal values; previously these errors would pass silently. For
1546 example, you can no longer set a handler on the :const:`SIGKILL` signal.
1547
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001548.. ======================================================================
Georg Brandl116aa622007-08-15 14:28:22 +00001549
1550
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001551.. _24acks:
Georg Brandl116aa622007-08-15 14:28:22 +00001552
1553Acknowledgements
1554================
1555
1556The author would like to thank the following people for offering suggestions,
1557corrections and assistance with various drafts of this article: Koray Can, Hye-
1558Shik Chang, Michael Dyck, Raymond Hettinger, Brian Hurt, Hamish Lawson, Fredrik
1559Lundh, Sean Reifschneider, Sadruddin Rejeb.
1560