1
 ?÷     ****************************
  What's New in Python 2.5
****************************

:Author: A.M. Kuchling

.. |release| replace:: 1.01

.. $Id: whatsnew25.tex 56611 2007-07-29 08:26:10Z georg.brandl $
.. Fix XXX comments

This article explains the new features in Python 2.5.  The final release of
Python 2.5 is scheduled for August 2006; :pep:`356` describes the planned
release schedule.

The changes in Python 2.5 are an interesting mix of language and library
improvements. The library enhancements will be more important to Python's user
community, I think, because several widely-useful packages were added.  New
modules include ElementTree for XML processing (:mod:`xml.etree`),
the SQLite database module (:mod:`sqlite`), and the :mod:`ctypes`
module for calling C functions.

The language changes are of middling significance.  Some pleasant new features
were added, but most of them aren't features that you'll use every day.
Conditional expressions were finally added to the language using a novel syntax;
see section :ref:`pep-308`.  The new ':keyword:`with`' statement will make
writing cleanup code easier (section :ref:`pep-343`).  Values can now be passed
into generators (section :ref:`pep-342`).  Imports are now visible as either
absolute or relative (section :ref:`pep-328`).  Some corner cases of exception
handling are handled better (section :ref:`pep-341`).  All these improvements
are worthwhile, but they're improvements to one specific language feature or
another; none of them are broad modifications to Python's semantics.

As well as the language and library additions, other improvements and bugfixes
were made throughout the source tree.  A search through the SVN change logs
finds there were 353 patches applied and 458 bugs fixed between Python 2.4 and
2.5.  (Both figures are likely to be underestimates.)

This article doesn't try to be a complete specification of the new features;
instead changes are briefly introduced using helpful examples.  For full
details, you should always refer to the documentation for Python 2.5 at
http://docs.python.org. If you want to understand the complete implementation
and design rationale, refer to the PEP for a particular new feature.

Comments, suggestions, and error reports for this document are welcome; please
e-mail them to the author or open a bug in the Python bug tracker.

.. ======================================================================


.. _pep-308:

PEP 308: Conditional Expressions
================================

For a long time, people have been requesting a way to write conditional
expressions, which are expressions that return value A or value B depending on
whether a Boolean value is true or false.  A conditional expression lets you
write a single assignment statement that has the same effect as the following::

   if condition:
       x = true_value
   else:
       x = false_value

There have been endless tedious discussions of syntax on both python-dev and
comp.lang.python.  A vote was even held that found the majority of voters wanted
conditional expressions in some form, but there was no syntax that was preferred
by a clear majority. Candidates included C's ``cond ? true_v : false_v``, ``if
cond then true_v else false_v``, and 16 other variations.

Guido van Rossum eventually chose a surprising syntax::

   x = true_value if condition else false_value

Evaluation is still lazy as in existing Boolean expressions, so the order of
evaluation jumps around a bit.  The *condition* expression in the middle is
evaluated first, and the *true_value* expression is evaluated only if the
condition was true.  Similarly, the *false_value* expression is only evaluated
when the condition is false.

This syntax may seem strange and backwards; why does the condition go in the
*middle* of the expression, and not in the front as in C's ``c ? x : y``?  The
decision was checked by applying the new syntax to the modules in the standard
library and seeing how the resulting code read.  In many cases where a
conditional expression is used, one value seems to be the 'c