f | **************************** |
n | What's New in Python 2.4 |
n | What's New in Python 2.4 |
| **************************** |
| |
| :Author: A.M. Kuchling |
| |
| .. |release| replace:: 1.02 |
| |
n | .. % $Id: whatsnew24.tex 50936 2006-07-29 15:42:46Z andrew.kuchling $ |
n | .. $Id: whatsnew24.tex 54632 2007-03-31 11:59:54Z georg.brandl $ |
| .. % Don't write extensive text for new sections; I'll do that. |
| .. Don't write extensive text for new sections; I'll do that. |
| .. % Feel free to add commented-out reminders of things that need |
| .. Feel free to add commented-out reminders of things that need |
| .. % to be covered. --amk |
| .. to be covered. --amk |
| |
| This article explains the new features in Python 2.4.1, released on March 30, |
| 2005. |
| |
| Python 2.4 is a medium-sized release. It doesn't introduce as many changes as |
| the radical Python 2.2, but introduces more features than the conservative 2.3 |
| release. The most significant new language features are function decorators and |
| generator expressions; most other changes are to the standard library. |
| |
| According to the CVS change logs, there were 481 patches applied and 502 bugs |
| fixed between Python 2.3 and 2.4. Both figures are likely to be underestimates. |
| |
| This article doesn't attempt to provide a complete specification of every single |
| new feature, but instead provides a brief introduction to each feature. For |
| full details, you should refer to the documentation for Python 2.4, such as the |
n | Python Library Reference (XXX reference: ../lib/lib.html) and the Python |
n | Python Library Reference and the Python Reference Manual. Often you will be |
| Reference Manual (XXX reference: ../ref/ref.html). Often you will be referred |
| to the PEP for a particular new feature for explanations of the implementation |
| referred to the PEP for a particular new feature for explanations of the |
| and design rationale. |
| implementation and design rationale. |
| |
n | .. % ====================================================================== |
n | .. ====================================================================== |
| |
| |
| PEP 218: Built-In Set Objects |
| ============================= |
| |
| Python 2.3 introduced the :mod:`sets` module. C implementations of set data |
| types have now been added to the Python core as two new built-in types, |
| :func:`set(iterable)` and :func:`frozenset(iterable)`. They provide high speed |
| >>> a & b # letters in both a and b |
| set(['a', 'c']) |
| >>> a ^ b # letters in a or b but not both |
| set(['r', 'd', 'b', 'm', 'z', 'l']) |
| |
| >>> a.add('z') # add a new element |
| >>> a.update('wxy') # add multiple new elements |
| >>> a |
n | set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'x', 'z']) |
n | set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'x', 'z']) |
| >>> a.remove('x') # take one element out |
| >>> a |
n | set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'z']) |
n | set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'z']) |
| |
| The :func:`frozenset` type is an immutable version of :func:`set`. Since it is |
| immutable and hashable, it may be used as a dictionary key or as a member of |
| another set. |
| |
| The :mod:`sets` module remains in the standard library, and may be useful if you |
| wish to subclass the :class:`Set` or :class:`ImmutableSet` classes. There are |
| currently no plans to deprecate the module. |
| |
| |
| .. seealso:: |
| |
| :pep:`218` - Adding a Built-In Set Object Type |
| Originally proposed by Greg Wilson and ultimately implemented by Raymond |
| Hettinger. |
| |
n | .. % ====================================================================== |
n | .. ====================================================================== |
| |
| |
| PEP 237: Unifying Long Integers and Integers |
| ============================================ |
| |
| The lengthy transition process for this PEP, begun in Python 2.2, takes another |
| step forward in Python 2.4. In 2.3, certain integer operations that would |
| behave differently after int/long unification triggered :exc:`FutureWarning` |
| |
| >>> import string |
| >>> t = string.Template('$page: $title') |
| >>> t.substitute({'page':2, 'title': 'The Best of Times'}) |
| '2: The Best of Times' |
| |
| If a key is missing from the dictionary, the :meth:`substitute` method will |
| raise a :exc:`KeyError`. There's also a :meth:`safe_substitute` method that |
n | ignores missing keys: |
n | ignores missing keys:: |
| |
| .. % $ Terminate $-mode for Emacs |
| |
| :: |
| |
| >>> t = string.Template('$page: $title') |
| >>> t.safe_substitute({'page':3}) |
| '3: $title' |
| |
n | .. % $ Terminate math-mode for Emacs |
| |
| |
| .. seealso:: |
| |
| :pep:`292` - Simpler String Substitutions |
| Written and implemented by Barry Warsaw. |
| |
n | .. % ====================================================================== |
n | .. ====================================================================== |
| |
| |
| PEP 318: Decorators for Functions and Methods |
| ============================================= |
| |
| Python 2.2 extended Python's object model by adding static methods and class |
| methods, but it didn't extend Python's syntax to provide any new way of defining |
| static or class methods. Instead, you had to write a :keyword:`def` statement |
| :pep:`318` - Decorators for Functions, Methods and Classes |
| Written by Kevin D. Smith, Jim Jewett, and Skip Montanaro. Several people |
| wrote patches implementing function decorators, but the one that was actually |
| checked in was patch #979728, written by Mark Russell. |
| |
| http://www.python.org/moin/PythonDecoratorLibrary |
| This Wiki page contains several examples of decorators. |
| |
n | .. % ====================================================================== |
n | .. ====================================================================== |
| |
| |
| PEP 322: Reverse Iteration |
| ========================== |
| |
| A new built-in function, :func:`reversed(seq)`, takes a sequence and returns an |
| iterator that loops over the elements of the sequence in reverse order. :: |
| |
| >>> for i in reversed(xrange(1,4)): |
| ... print i |
n | ... |
n | ... |
| 3 |
| 2 |
| 1 |
| |
| Compared to extended slicing, such as ``range(1,4)[::-1]``, :func:`reversed` is |
| easier to read, runs faster, and uses substantially less memory. |
| |
| Note that :func:`reversed` only accepts sequences, not arbitrary iterators. If |
| you want to reverse an iterator, first convert it to a list with :func:`list`. |
| :: |
| |
| >>> input = open('/etc/passwd', 'r') |
| >>> for line in reversed(list(input)): |
| ... print line |
n | ... |
n | ... |
| root:*:0:0:System Administrator:/var/root:/bin/tcsh |
| ... |
| |
| |
| .. seealso:: |
| |
| :pep:`322` - Reverse Iteration |
| Written and implemented by Raymond Hettinger. |
| |
n | .. % ====================================================================== |
n | .. ====================================================================== |
| |
| |
| PEP 324: New subprocess Module |
| ============================== |
| |
| The standard library provides a number of ways to execute a subprocess, offering |
| different features and different levels of complexity. |
| :func:`os.system(command)` is easy to use, but slow (it runs a shell process |
| is confusing. The :mod:`subprocess` module cleans this up, providing a unified |
| interface that offers all the features you might need. |
| |
| Instead of :mod:`popen2`'s collection of classes, :mod:`subprocess` contains a |
| single class called :class:`Popen` whose constructor supports a number of |
| different keyword arguments. :: |
| |
| class Popen(args, bufsize=0, executable=None, |
n | stdin=None, stdout=None, stderr=None, |
n | stdin=None, stdout=None, stderr=None, |
| preexec_fn=None, close_fds=False, shell=False, |
| preexec_fn=None, close_fds=False, shell=False, |
| cwd=None, env=None, universal_newlines=False, |
| cwd=None, env=None, universal_newlines=False, |
| startupinfo=None, creationflags=0): |
| startupinfo=None, creationflags=0): |
| |
| *args* is commonly a sequence of strings that will be the arguments to the |
| program executed as the subprocess. (If the *shell* argument is true, *args* |
| can be a string which will then be passed on to the shell for interpretation, |
| just as :func:`os.system` does.) |
| |
| *stdin*, *stdout*, and *stderr* specify what the subprocess's input, output, and |
| error streams will be. You can provide a file object or a file descriptor, or |
| you can change the properties of this context to alter the default precision, |
| rounding, or trap handling. The following example shows the effect of changing |
| the precision of the default context:: |
| |
| >>> decimal.getcontext().prec |
| 28 |
| >>> decimal.Decimal(1) / decimal.Decimal(7) |
| Decimal("0.1428571428571428571428571429") |
n | >>> decimal.getcontext().prec = 9 |
n | >>> decimal.getcontext().prec = 9 |
| >>> decimal.Decimal(1) / decimal.Decimal(7) |
| Decimal("0.142857143") |
| |
| The default action for error conditions is selectable; the module can either |
| return a special value such as infinity or not-a-number, or exceptions can be |
| raised:: |
| |
| >>> decimal.Decimal(1) / decimal.Decimal(0) |
| Traceback (most recent call last): |
| ... |
| decimal.DivisionByZero: x / 0 |
| >>> decimal.getcontext().traps[decimal.DivisionByZero] = False |
| >>> decimal.Decimal(1) / decimal.Decimal(0) |
| Decimal("Infinity") |
n | >>> |
n | >>> |
| |
| The :class:`Context` instance also has various methods for formatting numbers |
| such as :meth:`to_eng_string` and :meth:`to_sci_string`. |
| |
| For more information, see the documentation for the :mod:`decimal` module, which |
| includes a quick-start tutorial and a reference. |
| |
| |
| The article uses Fortran code to illustrate many of the problems that floating- |
| point inaccuracy can cause. |
| |
| http://www2.hursley.ibm.com/decimal/ |
| A description of a decimal-based representation. This representation is being |
| proposed as a standard, and underlies the new Python decimal type. Much of this |
| material was written by Mike Cowlishaw, designer of the Rexx language. |
| |
n | .. % ====================================================================== |
n | .. ====================================================================== |
| |
| |
| PEP 328: Multi-line Imports |
| =========================== |
| |
| One language change is a small syntactic tweak aimed at making it easier to |
| import many names from a module. In a ``from module import names`` statement, |
| *names* is a sequence of names separated by commas. If the sequence is very |
| |
| * :cfunc:`PyOS_ascii_strtod(str, ptr)` and :cfunc:`PyOS_ascii_atof(str, ptr)` |
| both convert a string to a C :ctype:`double`. |
| |
| * :cfunc:`PyOS_ascii_formatd(buffer, buf_len, format, d)` converts a |
| :ctype:`double` to an ASCII string. |
| |
| The code for these functions came from the GLib library |
n | (`<http://developer.gnome.org/arch/gtk/glib.html>`_), whose developers kindly |
n | (http://developer.gnome.org/arch/gtk/glib.html), whose developers kindly |
| relicensed the relevant functions and donated them to the Python Software |
| Foundation. The :mod:`locale` module can now change the numeric locale, |
| letting extensions such as GTK+ produce the correct results. |
| |
| |
| .. seealso:: |
| |
| :pep:`331` - Locale-Independent Float/String Conversions |
| Written by Christian R. Reis, and implemented by Gustavo Carneiro. |
| |
n | .. % ====================================================================== |
n | .. ====================================================================== |
| |
| |
| Other Language Changes |
| ====================== |
| |
| Here are all of the changes that Python 2.4 makes to the core Python language. |
| |
| * Decorators for functions and methods were added (:pep:`318`). |
| |
| * Strings also gained an :meth:`rsplit` method that works like the :meth:`split` |
| method but splits from the end of the string. (Contributed by Sean |
| Reifschneider.) :: |
| |
| >>> 'www.python.org'.split('.', 1) |
| ['www', 'python.org'] |
| 'www.python.org'.rsplit('.', 1) |
n | ['www.python', 'org'] |
n | ['www.python', 'org'] |
| |
| * Three keyword parameters, *cmp*, *key*, and *reverse*, were added to the |
| :meth:`sort` method of lists. These parameters make some common usages of |
| :meth:`sort` simpler. All of these parameters are optional. |
| |
| For the *cmp* parameter, the value should be a comparison function that takes |
| two parameters and returns -1, 0, or +1 depending on how the parameters compare. |
| This function will then be used to sort the list. Previously this was the only |
| * Encountering a failure while importing a module no longer leaves a partially- |
| initialized module object in ``sys.modules``. The incomplete module object left |
| behind would fool further imports of the same module into succeeding, leading to |
| confusing errors. (Fixed by Tim Peters.) |
| |
| * :const:`None` is now a constant; code that binds a new value to the name |
| ``None`` is now a syntax error. (Contributed by Raymond Hettinger.) |
| |
n | .. % ====================================================================== |
n | .. ====================================================================== |
| |
| |
| Optimizations |
| ------------- |
| |
| * The inner loops for list and tuple slicing were optimized and now run about |
| one-third faster. The inner loops for dictionaries were also optimized, |
| resulting in performance boosts for :meth:`keys`, :meth:`values`, :meth:`items`, |
| together. (Contributed by Armin Rigo.) |
| |
| The net result of the 2.4 optimizations is that Python 2.4 runs the pystone |
| benchmark around 5% faster than Python 2.3 and 35% faster than Python 2.2. |
| (pystone is not a particularly good benchmark, but it's the most commonly used |
| measurement of Python's performance. Your own applications may show greater or |
| smaller benefits from Python 2.4.) |
| |
n | .. % pystone is almost useless for comparing different versions of Python; |
n | .. pystone is almost useless for comparing different versions of Python; |
| .. % instead, it excels at predicting relative Python performance on |
| instead, it excels at predicting relative Python performance on different |
| .. % different machines. |
| .. % So, this section would be more informative if it used other tools |
| machines. So, this section would be more informative if it used other tools |
| .. % such as pybench and parrotbench. For a more application oriented |
| such as pybench and parrotbench. For a more application oriented benchmark, |
| .. % benchmark, try comparing the timings of test_decimal.py under 2.3 |
| try comparing the timings of test_decimal.py under 2.3 and 2.4. |
| .. % and 2.4. |
| |
n | .. % ====================================================================== |
n | .. ====================================================================== |
| |
| |
| New, Improved, and Deprecated Modules |
| ===================================== |
| |
| As usual, Python's standard library received a number of enhancements and bug |
| fixes. Here's a partial list of the most notable changes, sorted alphabetically |
| by module name. Consult the :file:`Misc/NEWS` file in the source tree for a more |
| deque(['f', 'g', 'h', 'i', 'j']) |
| >>> d.pop() # return and remove the rightmost item |
| 'j' |
| >>> d.popleft() # return and remove the leftmost item |
| 'f' |
| >>> list(d) # list the contents of the deque |
| ['g', 'h', 'i'] |
| >>> 'h' in d # search the deque |
n | True |
n | True |
| |
| Several modules, such as the :mod:`Queue` and :mod:`threading` modules, now take |
| advantage of :class:`collections.deque` for improved performance. (Contributed |
| by Raymond Hettinger.) |
| |
| * The :mod:`ConfigParser` classes have been enhanced slightly. The :meth:`read` |
| method now returns a list of the files that were successfully parsed, and the |
| :meth:`set` method raises :exc:`TypeError` if passed a *value* argument that |
| Here's an example to make this clearer. The *key* function simply returns |
| whether a number is even or odd, so the result of :func:`groupby` is to return |
| consecutive runs of odd or even numbers. :: |
| |
| >>> import itertools |
| >>> L = [2, 4, 6, 7, 8, 9, 11, 12, 14] |
| >>> for key_val, it in itertools.groupby(L, lambda x: x % 2): |
| ... print key_val, list(it) |
n | ... |
n | ... |
| 0 [2, 4, 6] |
| 1 [7] |
| 0 [8] |
| 1 [9, 11] |
| 0 [12, 14] |
n | >>> |
n | >>> |
| |
| :func:`groupby` is typically used with sorted input. The logic for |
| :func:`groupby` is similar to the Unix ``uniq`` filter which makes it handy for |
| eliminating, counting, or identifying duplicate elements:: |
| |
| >>> word = 'abracadabra' |
| >>> letters = sorted(word) # Turn string into a sorted list of letters |
n | >>> letters |
n | >>> letters |
| ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'c', 'd', 'r', 'r'] |
| >>> for k, g in itertools.groupby(letters): |
| ... print k, list(g) |
n | ... |
n | ... |
| a ['a', 'a', 'a', 'a', 'a'] |
| b ['b', 'b'] |
| c ['c'] |
| d ['d'] |
| r ['r', 'r'] |
| >>> # List unique letters |
n | >>> [k for k, g in groupby(letters)] |
n | >>> [k for k, g in groupby(letters)] |
| ['a', 'b', 'c', 'd', 'r'] |
| >>> # Count letter occurrences |
n | >>> [(k, len(list(g))) for k, g in groupby(letters)] |
n | >>> [(k, len(list(g))) for k, g in groupby(letters)] |
| [('a', 5), ('b', 2), ('c', 1), ('d', 1), ('r', 2)] |
| |
| (Contributed by Hye-Shik Chang.) |
| |
| * :mod:`itertools` also gained a function named :func:`tee(iterator, N)` that |
| returns *N* independent iterators that replicate *iterator*. If *N* is omitted, |
| the default is 2. :: |
| |
| >>> L = [1,2,3] |
| >>> i1, i2 = itertools.tee(L) |
| >>> i1,i2 |
| (<itertools.tee object at 0x402c2080>, <itertools.tee object at 0x402c2090>) |
| >>> list(i1) # Run the first iterator to exhaustion |
| [1, 2, 3] |
| >>> list(i2) # Run the second iterator to exhaustion |
| [1, 2, 3] |
n | > |
n | |
| Note that :func:`tee` has to keep copies of the values returned by the |
| iterator; in the worst case, it may need to keep all of them. This should |
| therefore be used carefully if the leading iterator can run far ahead of the |
| trailing iterator in a long stream of inputs. If the separation is large, then |
| you might as well use :func:`list` instead. When the iterators track closely |
| with one another, :func:`tee` is ideal. Possible applications include |
| bookmarking, windowing, or lookahead iterators. (Contributed by Raymond |
| Hettinger.) |
| :func:`basicConfig` function to simplify log configuration. The default |
| behavior is to log messages to standard error, but various keyword arguments can |
| be specified to log to a particular file, change the logging format, or set the |
| logging level. For example:: |
| |
| import logging |
| logging.basicConfig(filename='/var/log/application.log', |
| level=0, # Log all messages |
n | format='%(levelname):%(process):%(thread):%(message)') |
n | format='%(levelname):%(process):%(thread):%(message)') |
| |
| Other additions to the :mod:`logging` package include a :meth:`log(level, msg)` |
| convenience method, as well as a :class:`TimedRotatingFileHandler` class that |
| rotates its log files at a timed interval. The module already had |
| :class:`RotatingFileHandler`, which rotated logs once the file exceeded a |
| certain size. Both classes derive from a new :class:`BaseRotatingHandler` class |
| that can be used to implement other rotating handlers. |
| |
| |
| * The :mod:`xmlrpclib` module now supports a multi-call extension for |
| transmitting multiple XML-RPC calls in a single HTTP operation. (Contributed by |
| Brian Quinlan.) |
| |
| * The :mod:`mpz`, :mod:`rotor`, and :mod:`xreadlines` modules have been |
| removed. |
| |
n | .. % ====================================================================== |
n | .. ====================================================================== |
| .. % whole new modules get described in subsections here |
| .. whole new modules get described in subsections here |
| .. % ===================== |
| .. ===================== |
| |
| |
| cookielib |
| --------- |
| |
| The :mod:`cookielib` library supports client-side handling for HTTP cookies, |
| mirroring the :mod:`Cookie` module's server-side cookie support. Cookies are |
| stored in cookie jars; the library transparently stores cookies offered by the |
| L = 'here is a rather lengthy list of words'.split() |
| for word in L[:n]: |
| print word |
| |
| Running the above function's tests with :const:`doctest.REPORT_UDIFF` specified, |
| you get the following output:: |
| |
| ********************************************************************** |
n | File ``t.py'', line 15, in g |
n | File "t.py", line 15, in g |
| Failed example: |
| g(4) |
| Differences (unified diff with -expected +actual): |
| @@ -2,3 +2,3 @@ |
| is |
| a |
| -lengthy |
| +rather |
| ********************************************************************** |
| |
n | .. % ====================================================================== |
n | .. ====================================================================== |
| |
| |
| Build and C API Changes |
| ======================= |
| |
| Some of the changes to Python's build process and to the C API are: |
| |
| * Three new convenience macros were added for common return values from |
| Counter register. Note that the :option:`----with-tsc` switch is slightly |
| misnamed, because the profiling feature also works on the PowerPC platform, |
| though that processor architecture doesn't call that register "the TSC |
| register". (Contributed by Jeremy Hylton.) |
| |
| * The :ctype:`tracebackobject` type has been renamed to |
| :ctype:`PyTracebackObject`. |
| |
n | .. % ====================================================================== |
n | .. ====================================================================== |
| |
| |
| Port-Specific Changes |
| --------------------- |
| |
| * The Windows port now builds under MSVC++ 7.1 as well as version 6. |
| (Contributed by Martin von Löwis.) |
| |
n | .. % ====================================================================== |
n | .. ====================================================================== |
| |
| |
| Porting to Python 2.4 |
| ===================== |
| |
| This section lists previously described changes that may require changes to your |
| code: |
| |
| |
| * :const:`None` is now a constant; code that binds a new value to the name |
| ``None`` is now a syntax error. |
| |
| * The :func:`signals.signal` function now raises a :exc:`RuntimeError` exception |
| for certain illegal values; previously these errors would pass silently. For |
| example, you can no longer set a handler on the :const:`SIGKILL` signal. |
| |
n | .. % ====================================================================== |
n | .. ====================================================================== |
| |
| |
t | .. _acks: |
t | .. _24acks: |
| |
| Acknowledgements |
| ================ |
| |
| The author would like to thank the following people for offering suggestions, |
| corrections and assistance with various drafts of this article: Koray Can, Hye- |
| Shik Chang, Michael Dyck, Raymond Hettinger, Brian Hurt, Hamish Lawson, Fredrik |
| Lundh, Sean Reifschneider, Sadruddin Rejeb. |