f | **************************** |
n | What's New in Python 2.1 |
n | What's New in Python 2.1 |
| **************************** |
| |
| :Author: A.M. Kuchling |
| |
| .. |release| replace:: 1.01 |
| |
n | .. % $Id: whatsnew21.tex 50964 2006-07-30 03:03:43Z fred.drake $ |
n | .. $Id: whatsnew21.tex 50964 2006-07-30 03:03:43Z fred.drake $ |
| |
| |
| Introduction |
| ============ |
| |
| This article explains the new features in Python 2.1. While there aren't as |
| many changes in 2.1 as there were in Python 2.0, there are still some pleasant |
| surprises in store. 2.1 is the first release to be steered through the use of |
| |
| One recent goal of the Python development team has been to accelerate the pace |
| of new releases, with a new release coming every 6 to 9 months. 2.1 is the first |
| release to come out at this faster pace, with the first alpha appearing in |
| January, 3 months after the final version of 2.0 was released. |
| |
| The final release of Python 2.1 was made on April 17, 2001. |
| |
n | .. % ====================================================================== |
n | .. ====================================================================== |
| |
| |
| PEP 227: Nested Scopes |
| ====================== |
| |
| The largest change in Python 2.1 is to Python's scoping rules. In Python 2.0, |
| at any given time there are at most three namespaces used to look up variable |
| names: local, module-level, and the built-in namespace. This often surprised |
| definitions or :keyword:`lambda` expressions with free variables, the compiler |
| will flag this by raising a :exc:`SyntaxError` exception. |
| |
| To make the preceding explanation a bit clearer, here's an example:: |
| |
| x = 1 |
| def f(): |
| # The next line is a syntax error |
n | exec 'x=2' |
n | exec 'x=2' |
| def g(): |
| return x |
| |
| Line 4 containing the :keyword:`exec` statement is a syntax error, since |
| :keyword:`exec` would define a new local variable named ``x`` whose value should |
| be accessed by :func:`g`. |
| |
| This shouldn't be much of a limitation, since :keyword:`exec` is rarely used in |
| all of 2.1's lifetime to fix any breakage resulting from their introduction. |
| |
| |
| .. seealso:: |
| |
| :pep:`227` - Statically Nested Scopes |
| Written and implemented by Jeremy Hylton. |
| |
n | .. % ====================================================================== |
n | .. ====================================================================== |
| |
| |
| PEP 236: __future__ Directives |
| ============================== |
| |
| The reaction to nested scopes was widespread concern about the dangers of |
| breaking code with the 2.1 release, and it was strong enough to make the |
| Pythoneers take a more conservative approach. This approach consists of |
| precede any statement that will result in bytecodes being produced. |
| |
| |
| .. seealso:: |
| |
| :pep:`236` - Back to the :mod:`__future__` |
| Written by Tim Peters, and primarily implemented by Jeremy Hylton. |
| |
n | .. % ====================================================================== |
n | .. ====================================================================== |
| |
| |
| PEP 207: Rich Comparisons |
| ========================= |
| |
| In earlier versions, Python's support for implementing comparisons on user- |
| defined classes and extension types was quite simple. Classes could implement a |
| :meth:`__cmp__` method that was given two instances of a class, and could only |
| |
| |
| .. seealso:: |
| |
| :pep:`207` - Rich Comparisions |
| Written by Guido van Rossum, heavily based on earlier work by David Ascher, and |
| implemented by Guido van Rossum. |
| |
n | .. % ====================================================================== |
n | .. ====================================================================== |
| |
| |
| PEP 230: Warning Framework |
| ========================== |
| |
| Over its 10 years of existence, Python has accumulated a certain number of |
| obsolete modules and features along the way. It's difficult to know when a |
| feature is safe to remove, since there's no way of knowing how much code uses it |
| Written by Paul Prescod, to specify procedures to be followed when removing old |
| features from Python. The policy described in this PEP hasn't been officially |
| adopted, but the eventual policy probably won't be too different from Prescod's |
| proposal. |
| |
| :pep:`230` - Warning Framework |
| Written and implemented by Guido van Rossum. |
| |
n | .. % ====================================================================== |
n | .. ====================================================================== |
| |
| |
| PEP 229: New Build System |
| ========================= |
| |
| When compiling Python, the user had to go in and edit the :file:`Modules/Setup` |
| file in order to enable various additional modules; the default set is |
| relatively small and limited to modules that compile on most Unix platforms. |
| proxy.attr # raises weakref.ReferenceError |
| |
| |
| .. seealso:: |
| |
| :pep:`205` - Weak References |
| Written and implemented by Fred L. Drake, Jr. |
| |
n | .. % ====================================================================== |
n | .. ====================================================================== |
| |
| |
| PEP 232: Function Attributes |
| ============================ |
| |
| In Python 2.1, functions can now have arbitrary information attached to them. |
| People were often using docstrings to hold information about functions and |
| methods, because the ``__doc__`` attribute was the only way of attaching any |
| that behaves like a mapping. |
| |
| |
| .. seealso:: |
| |
| :pep:`232` - Function Attributes |
| Written and implemented by Barry Warsaw. |
| |
n | .. % ====================================================================== |
n | .. ====================================================================== |
| |
| |
| PEP 235: Importing Modules on Case-Insensitive Platforms |
| ======================================================== |
| |
| Some operating systems have filesystems that are case-insensitive, MacOS and |
| Windows being the primary examples; on these systems, it's impossible to |
| distinguish the filenames ``FILE.PY`` and ``file.py``, even though they do store |
| |
| In Python 2.1, the :keyword:`import` statement will work to simulate case- |
| sensitivity on case-insensitive platforms. Python will now search for the first |
| case-sensitive match by default, raising an :exc:`ImportError` if no such file |
| is found, so ``import file`` will not import a module named ``FILE.PY``. Case- |
| insensitive matching can be requested by setting the :envvar:`PYTHONCASEOK` |
| environment variable before starting the Python interpreter. |
| |
n | .. % ====================================================================== |
n | .. ====================================================================== |
| |
| |
| PEP 217: Interactive Display Hook |
| ================================= |
| |
| When using the Python interpreter interactively, the output of commands is |
| displayed using the built-in :func:`repr` function. In Python 2.1, the variable |
| :func:`sys.displayhook` can be set to a callable object which will be called |
| >>> |
| |
| |
| .. seealso:: |
| |
| :pep:`217` - Display Hook for Interactive Use |
| Written and implemented by Moshe Zadka. |
| |
n | .. % ====================================================================== |
n | .. ====================================================================== |
| |
| |
| PEP 208: New Coercion Model |
| =========================== |
| |
| How numeric coercion is done at the C level was significantly modified. This |
| will only affect the authors of C extensions to Python, allowing them more |
| flexibility in writing extension types that support numeric operations. |
| |
| .. seealso:: |
| |
| :pep:`208` - Reworking the Coercion Model |
| Written and implemented by Neil Schemenauer, heavily based upon earlier work by |
| Marc-André Lemburg. Read this to understand the fine points of how numeric |
| operations will now be processed at the C level. |
| |
n | .. % ====================================================================== |
n | .. ====================================================================== |
| |
| |
| PEP 241: Metadata in Python Packages |
| ==================================== |
| |
| A common complaint from Python users is that there's no single catalog of all |
| the Python modules in existence. T. Middleton's Vaults of Parnassus at |
n | `<http://www.vex.net/parnassus/>`_ are the largest catalog of Python modules, |
n | http://www.vex.net/parnassus/ are the largest catalog of Python modules, but |
| but registering software at the Vaults is optional, and many people don't |
| registering software at the Vaults is optional, and many people don't bother. |
| bother. |
| |
| As a first small step toward fixing the problem, Python software packaged using |
| the Distutils :command:`sdist` command will include a file named |
| :file:`PKG-INFO` containing information about the package such as its name, |
| version, and author (metadata, in cataloguing terminology). PEP 241 contains |
| the full list of fields that can be present in the :file:`PKG-INFO` file. As |
| people began to package their software using Python 2.1, more and more packages |
| will include metadata, making it possible to build automated cataloguing systems |
| For example, the Distutils :command:`sdist` and :command:`bdist_\*` commands |
| could support a :option:`upload` option that would automatically upload your |
| package to a catalog server. |
| |
| You can start creating packages containing :file:`PKG-INFO` even if you're not |
| using Python 2.1, since a new release of the Distutils will be made for users of |
| earlier Python versions. Version 1.0.2 of the Distutils includes the changes |
| described in PEP 241, as well as various bugfixes and enhancements. It will be |
n | available from the Distutils SIG at |
n | available from the Distutils SIG at http://www.python.org/sigs/distutils-sig/. |
| `<http://www.python.org/sigs/distutils-sig/>`_. |
| |
| |
| .. seealso:: |
| |
| :pep:`241` - Metadata for Python Software Packages |
| Written and implemented by A.M. Kuchling. |
| |
| :pep:`243` - Module Repository Upload Mechanism |
| Written by Sean Reifschneider, this draft PEP describes a proposed mechanism for |
| uploading Python packages to a central server. |
| |
n | .. % ====================================================================== |
n | .. ====================================================================== |
| |
| |
| New and Improved Modules |
| ======================== |
| |
| * Ka-Ping Yee contributed two new modules: :mod:`inspect.py`, a module for |
| getting information about live Python code, and :mod:`pydoc.py`, a module for |
| interactively converting docstrings to HTML or text. As a bonus, |
| :file:`pydoc` also includes a Tk-based interactive help browser. :file:`pydoc` |
| quickly becomes addictive; try it out! |
| |
| * Two different modules for unit testing were added to the standard library. |
| The :mod:`doctest` module, contributed by Tim Peters, provides a testing |
| framework based on running embedded examples in docstrings and comparing the |
| results against the expected output. PyUnit, contributed by Steve Purcell, is a |
| unit testing framework inspired by JUnit, which was in turn an adaptation of |
n | Kent Beck's Smalltalk testing framework. See |
n | Kent Beck's Smalltalk testing framework. See http://pyunit.sourceforge.net/ for |
| `<http://pyunit.sourceforge.net/>`_ for more information about PyUnit. |
| more information about PyUnit. |
| |
| * The :mod:`difflib` module contains a class, :class:`SequenceMatcher`, which |
| compares two sequences and computes the changes required to transform one |
| sequence into the other. For example, this module can be used to write a tool |
| similar to the Unix :program:`diff` program, and in fact the sample program |
| :file:`Tools/scripts/ndiff.py` demonstrates how to write such a script. |
| |
| * :mod:`curses.panel`, a wrapper for the panel library, part of ncurses and of |
| optional integer argument *depth* is supplied, the function returns the frame |
| that is *depth* calls below the top of the stack. For example, |
| ``sys._getframe(1)`` returns the caller's frame object. |
| |
| This function is only present in CPython, not in Jython or the .NET |
| implementation. Use it for debugging, and resist the temptation to put it into |
| production code. |
| |
n | .. % ====================================================================== |
n | .. ====================================================================== |
| |
| |
| Other Changes and Fixes |
| ======================= |
| |
| There were relatively few smaller changes made in Python 2.1 due to the shorter |
| release cycle. A search through the CVS change logs turns up 117 patches |
| applied, and 136 bugs fixed; both figures are likely to be underestimates. Some |
| not reading the entire file into memory as the existing :meth:`readlines` method |
| does. You'd use it like this:: |
| |
| for line in sys.stdin.xreadlines(): |
| # ... do something for each line ... |
| ... |
| |
| For a fuller discussion of the line I/O changes, see the python-dev summary for |
n | January 1-15, 2001 at `<http://www.python.org/dev/summary/2001-01-1.html>`_. |
n | January 1-15, 2001 at http://www.python.org/dev/summary/2001-01-1.html. |
| |
| * A new method, :meth:`popitem`, was added to dictionaries to enable |
| destructively iterating through the contents of a dictionary; this can be faster |
| for large dictionaries because there's no need to construct a list containing |
| all the keys or values. ``D.popitem()`` removes a random ``(key, value)`` pair |
| from the dictionary ``D`` and returns it as a 2-tuple. This was implemented |
| mostly by Tim Peters and Guido van Rossum, after a suggestion and preliminary |
| patch by Moshe Zadka. |
| |
| # List public names |
| __all__ = ['Database', 'open'] |
| |
| A stricter version of this patch was first suggested and implemented by Ben |
| Wolfson, but after some python-dev discussion, a weaker final version was |
| checked in. |
| |
n | * Applying :func:`repr` to strings previously used octal escapes for non- |
n | * Applying :func:`repr` to strings previously used octal escapes for |
| printable characters; for example, a newline was ``'\012'``. This was a |
| non-printable characters; for example, a newline was ``'\012'``. This was a |
| vestigial trace of Python's C ancestry, but today octal is of very little |
| practical use. Ka-Ping Yee suggested using hex escapes instead of octal ones, |
n | and using the ``\n``, ``\t``, ``\r`` escapes for the appropriate characters, and |
n | and using the ``\n``, ``\t``, ``\r`` escapes for the appropriate characters, |
| implemented this new formatting. |
| and implemented this new formatting. |
| |
| * Syntax errors detected at compile-time can now raise exceptions containing the |
| filename and line number of the error, a pleasant side effect of the compiler |
| reorganization done by Jeremy Hylton. |
| |
| * C extensions which import other modules have been changed to use |
| :func:`PyImport_ImportModule`, which means that they will use any import hooks |
| that have been installed. This is also encouraged for third-party extensions |
| * Some new ports were contributed: MacOS X (by Steven Majewski), Cygwin (by |
| Jason Tishler); RISCOS (by Dietmar Schwertberger); Unixware 7 (by Billy G. |
| Allie). |
| |
| And there's the usual list of minor bugfixes, minor memory leaks, docstring |
| edits, and other tweaks, too lengthy to be worth itemizing; see the CVS logs for |
| the full details if you want them. |
| |
t | .. % ====================================================================== |
t | .. ====================================================================== |
| |
| |
| Acknowledgements |
| ================ |
| |
| The author would like to thank the following people for offering suggestions on |
| various drafts of this article: Graeme Cross, David Goodger, Jay Graves, Michael |
| Hudson, Marc-André Lemburg, Fredrik Lundh, Neil Schemenauer, Thomas Wouters. |