What’s New in Python 2.5 — Python 3.16.0a1 documentation
Navigation index modules | next | previous | Python » 3.16.0a1 Documentation » What’s New in Python » What’s New in Python 2.5 | Theme | What’s New in Python 2.5 ¶ Author : A.M. Kuchling 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. Python 2.5 was released on September 19, 2006. 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 ( xml.etree.ElementTree ), the SQLite database module ( sqlite3 ), and the 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 PEP 308: Conditional Expressions . The new ‘ with ’ statement will make writing cleanup code easier (section PEP 343: The ‘with’ statement ). Values can now be passed into generators (section PEP 342: New Generator Features ). Imports are now visible as either absolute or relative (section PEP 328: Absolute and Relative Imports ).
Some corner cases of exception handling are handled better (section PEP 341: Unified try/except/finally ). 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 https://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: 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 ‘common case’ and one value is an ‘exceptional case’, used only on rarer occasions when the condition isn’t met.
The conditional syntax makes this pattern a bit more obvious: contents = (( doc + ' \n ' ) if doc else '' ) I read the above statement as meaning “here contents is usually assigned a value of doc+'\n' ; sometimes doc is empty, in which special case an empty string is returned.” I doubt I will use conditional expressions very often where there isn’t a clear common and uncommon case. There was some discussion of whether the language should require surrounding conditional expressions with parentheses.
The decision was made to not require parentheses in the Python language’s grammar, but as a matter of style I think you should always use them. Consider these two statements: # First version -- no parens level = 1 if logging else 0 # Second version -- with parens level = ( 1 if logging else 0 ) In the first version, I think a reader’s eye might group the statement into ‘level = 1’, ‘if logging’, ‘else 0’, and think that the condition decides whether the assignment to level is performed.
The second version reads better, in my opinion, because it makes it clear that the assignment is always performed and the choice is being made between two values. Another reason for including the brackets: a few odd combinations of list comprehensions and lambdas could look like incorrect conditional expressions. See PEP 308 for some examples. If you put parentheses around your conditional expressions, you won’t run into this case.
See also PEP 308 - Conditional Expressions PEP written by Guido van Rossum and Raymond D. Hettinger; implemented by Thomas Wouters. PEP 309: Partial Function Application ¶ The functools module is intended to contain tools for functional-style programming. One useful tool in this module is the partial() function. For programs written in a functional style, you’ll sometimes want to construct variants of existing functions that have some of the parameters filled in.
Consider a Python function f(a, b, c) ; you could create a new function g(b, c) that was equivalent to f(1, b, c) . This is called “partial function application”. partial() takes the arguments (function, arg1, arg2, ... kwarg1=value1, kwarg2=value2) . The resulting object is callable, so you can just call it to invoke function with the filled-in arguments. Here’s a small but realistic example: import functools def log ( message , subsystem ): "Write the contents of 'message' to the specified subsystem." print ' %s : %s ' % ( subsystem , message ) ... server_log = functools . partial ( log , subsystem = 'server' ) server_log ( 'Unable to open socket' ) Here’s another example, from a program that uses PyGTK.
Here a context-sensitive pop-up menu is being constructed dynamically. The callback provided for the menu option is a partially applied version of the open_item() method, where the first argument has been provided. ... class Application : def open_item ( self , path ): ... def init ( self ): open_func = functools . partial ( self . open_item , item_path ) popup_menu . append ( ( "Open" , open_func , 1 ) ) Another function in the functools module is the update_wrapper(wrapper, wrapped) function that helps you write well-behaved decorators. update_wrapper() copies the name, module, and docstring attribute to a wrapper function so that tracebacks inside the wrapped function are easier to understand.
For example, you might write: def my_decorator ( f ): def wrapper ( * args , ** kwds ): print 'Calling decorated function' return f ( * args , ** kwds ) functools . update_wrapper ( wrapper , f ) return wrapper wraps() is a decorator that can be used inside your own decorators to copy the wrapped function’s information. An alternate version of the previous example would be: def my_decorator ( f ): @functools . wraps ( f ) def wrapper ( * args , ** kwds ): print 'Calling decorated function' r
The rest of this page is on the live site.