| 1 | \documentclass{howto}
|
|---|
| 2 |
|
|---|
| 3 | \usepackage{distutils}
|
|---|
| 4 |
|
|---|
| 5 | % $Id: whatsnew21.tex 50964 2006-07-30 03:03:43Z fred.drake $
|
|---|
| 6 |
|
|---|
| 7 | \title{What's New in Python 2.1}
|
|---|
| 8 | \release{1.01}
|
|---|
| 9 | \author{A.M. Kuchling}
|
|---|
| 10 | \authoraddress{
|
|---|
| 11 | \strong{Python Software Foundation}\\
|
|---|
| 12 | Email: \email{[email protected]}
|
|---|
| 13 | }
|
|---|
| 14 | \begin{document}
|
|---|
| 15 | \maketitle\tableofcontents
|
|---|
| 16 |
|
|---|
| 17 | \section{Introduction}
|
|---|
| 18 |
|
|---|
| 19 | This article explains the new features in Python 2.1. While there aren't as
|
|---|
| 20 | many changes in 2.1 as there were in Python 2.0, there are still some
|
|---|
| 21 | pleasant surprises in store. 2.1 is the first release to be steered
|
|---|
| 22 | through the use of Python Enhancement Proposals, or PEPs, so most of
|
|---|
| 23 | the sizable changes have accompanying PEPs that provide more complete
|
|---|
| 24 | documentation and a design rationale for the change. This article
|
|---|
| 25 | doesn't attempt to document the new features completely, but simply
|
|---|
| 26 | provides an overview of the new features for Python programmers.
|
|---|
| 27 | Refer to the Python 2.1 documentation, or to the specific PEP, for
|
|---|
| 28 | more details about any new feature that particularly interests you.
|
|---|
| 29 |
|
|---|
| 30 | One recent goal of the Python development team has been to accelerate
|
|---|
| 31 | the pace of new releases, with a new release coming every 6 to 9
|
|---|
| 32 | months. 2.1 is the first release to come out at this faster pace, with
|
|---|
| 33 | the first alpha appearing in January, 3 months after the final version
|
|---|
| 34 | of 2.0 was released.
|
|---|
| 35 |
|
|---|
| 36 | The final release of Python 2.1 was made on April 17, 2001.
|
|---|
| 37 |
|
|---|
| 38 | %======================================================================
|
|---|
| 39 | \section{PEP 227: Nested Scopes}
|
|---|
| 40 |
|
|---|
| 41 | The largest change in Python 2.1 is to Python's scoping rules. In
|
|---|
| 42 | Python 2.0, at any given time there are at most three namespaces used
|
|---|
| 43 | to look up variable names: local, module-level, and the built-in
|
|---|
| 44 | namespace. This often surprised people because it didn't match their
|
|---|
| 45 | intuitive expectations. For example, a nested recursive function
|
|---|
| 46 | definition doesn't work:
|
|---|
| 47 |
|
|---|
| 48 | \begin{verbatim}
|
|---|
| 49 | def f():
|
|---|
| 50 | ...
|
|---|
| 51 | def g(value):
|
|---|
| 52 | ...
|
|---|
| 53 | return g(value-1) + 1
|
|---|
| 54 | ...
|
|---|
| 55 | \end{verbatim}
|
|---|
| 56 |
|
|---|
| 57 | The function \function{g()} will always raise a \exception{NameError}
|
|---|
| 58 | exception, because the binding of the name \samp{g} isn't in either
|
|---|
| 59 | its local namespace or in the module-level namespace. This isn't much
|
|---|
| 60 | of a problem in practice (how often do you recursively define interior
|
|---|
| 61 | functions like this?), but this also made using the \keyword{lambda}
|
|---|
| 62 | statement clumsier, and this was a problem in practice. In code which
|
|---|
| 63 | uses \keyword{lambda} you can often find local variables being copied
|
|---|
| 64 | by passing them as the default values of arguments.
|
|---|
| 65 |
|
|---|
| 66 | \begin{verbatim}
|
|---|
| 67 | def find(self, name):
|
|---|
| 68 | "Return list of any entries equal to 'name'"
|
|---|
| 69 | L = filter(lambda x, name=name: x == name,
|
|---|
| 70 | self.list_attribute)
|
|---|
| 71 | return L
|
|---|
| 72 | \end{verbatim}
|
|---|
| 73 |
|
|---|
| 74 | The readability of Python code written in a strongly functional style
|
|---|
| 75 | suffers greatly as a result.
|
|---|
| 76 |
|
|---|
| 77 | The most significant change to Python 2.1 is that static scoping has
|
|---|
| 78 | been added to the language to fix this problem. As a first effect,
|
|---|
| 79 | the \code{name=name} default argument is now unnecessary in the above
|
|---|
| 80 | example. Put simply, when a given variable name is not assigned a
|
|---|
| 81 | value within a function (by an assignment, or the \keyword{def},
|
|---|
| 82 | \keyword{class}, or \keyword{import} statements), references to the
|
|---|
| 83 | variable will be looked up in the local namespace of the enclosing
|
|---|
| 84 | scope. A more detailed explanation of the rules, and a dissection of
|
|---|
| 85 | the implementation, can be found in the PEP.
|
|---|
| 86 |
|
|---|
| 87 | This change may cause some compatibility problems for code where the
|
|---|
| 88 | same variable name is used both at the module level and as a local
|
|---|
| 89 | variable within a function that contains further function definitions.
|
|---|
| 90 | This seems rather unlikely though, since such code would have been
|
|---|
| 91 | pretty confusing to read in the first place.
|
|---|
| 92 |
|
|---|
| 93 | One side effect of the change is that the \code{from \var{module}
|
|---|
| 94 | import *} and \keyword{exec} statements have been made illegal inside
|
|---|
| 95 | a function scope under certain conditions. The Python reference
|
|---|
| 96 | manual has said all along that \code{from \var{module} import *} is
|
|---|
| 97 | only legal at the top level of a module, but the CPython interpreter
|
|---|
| 98 | has never enforced this before. As part of the implementation of
|
|---|
| 99 | nested scopes, the compiler which turns Python source into bytecodes
|
|---|
| 100 | has to generate different code to access variables in a containing
|
|---|
| 101 | scope. \code{from \var{module} import *} and \keyword{exec} make it
|
|---|
| 102 | impossible for the compiler to figure this out, because they add names
|
|---|
| 103 | to the local namespace that are unknowable at compile time.
|
|---|
| 104 | Therefore, if a function contains function definitions or
|
|---|
| 105 | \keyword{lambda} expressions with free variables, the compiler will
|
|---|
| 106 | flag this by raising a \exception{SyntaxError} exception.
|
|---|
| 107 |
|
|---|
| 108 | To make the preceding explanation a bit clearer, here's an example:
|
|---|
| 109 |
|
|---|
| 110 | \begin{verbatim}
|
|---|
| 111 | x = 1
|
|---|
| 112 | def f():
|
|---|
| 113 | # The next line is a syntax error
|
|---|
| 114 | exec 'x=2'
|
|---|
| 115 | def g():
|
|---|
| 116 | return x
|
|---|
| 117 | \end{verbatim}
|
|---|
| 118 |
|
|---|
| 119 | Line 4 containing the \keyword{exec} statement is a syntax error,
|
|---|
| 120 | since \keyword{exec} would define a new local variable named \samp{x}
|
|---|
| 121 | whose value should be accessed by \function{g()}.
|
|---|
| 122 |
|
|---|
| 123 | This shouldn't be much of a limitation, since \keyword{exec} is rarely
|
|---|
| 124 | used in most Python code (and when it is used, it's often a sign of a
|
|---|
| 125 | poor design anyway).
|
|---|
| 126 |
|
|---|
| 127 | Compatibility concerns have led to nested scopes being introduced
|
|---|
| 128 | gradually; in Python 2.1, they aren't enabled by default, but can be
|
|---|
| 129 | turned on within a module by using a future statement as described in
|
|---|
| 130 | PEP 236. (See the following section for further discussion of PEP
|
|---|
| 131 | 236.) In Python 2.2, nested scopes will become the default and there
|
|---|
| 132 | will be no way to turn them off, but users will have had all of 2.1's
|
|---|
| 133 | lifetime to fix any breakage resulting from their introduction.
|
|---|
| 134 |
|
|---|
| 135 | \begin{seealso}
|
|---|
| 136 |
|
|---|
| 137 | \seepep{227}{Statically Nested Scopes}{Written and implemented by
|
|---|
| 138 | Jeremy Hylton.}
|
|---|
| 139 |
|
|---|
| 140 | \end{seealso}
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 | %======================================================================
|
|---|
| 144 | \section{PEP 236: __future__ Directives}
|
|---|
| 145 |
|
|---|
| 146 | The reaction to nested scopes was widespread concern about the dangers
|
|---|
| 147 | of breaking code with the 2.1 release, and it was strong enough to
|
|---|
| 148 | make the Pythoneers take a more conservative approach. This approach
|
|---|
| 149 | consists of introducing a convention for enabling optional
|
|---|
| 150 | functionality in release N that will become compulsory in release N+1.
|
|---|
| 151 |
|
|---|
| 152 | The syntax uses a \code{from...import} statement using the reserved
|
|---|
| 153 | module name \module{__future__}. Nested scopes can be enabled by the
|
|---|
| 154 | following statement:
|
|---|
| 155 |
|
|---|
| 156 | \begin{verbatim}
|
|---|
| 157 | from __future__ import nested_scopes
|
|---|
| 158 | \end{verbatim}
|
|---|
| 159 |
|
|---|
| 160 | While it looks like a normal \keyword{import} statement, it's not;
|
|---|
| 161 | there are strict rules on where such a future statement can be put.
|
|---|
| 162 | They can only be at the top of a module, and must precede any Python
|
|---|
| 163 | code or regular \keyword{import} statements. This is because such
|
|---|
| 164 | statements can affect how the Python bytecode compiler parses code and
|
|---|
| 165 | generates bytecode, so they must precede any statement that will
|
|---|
| 166 | result in bytecodes being produced.
|
|---|
| 167 |
|
|---|
| 168 | \begin{seealso}
|
|---|
| 169 |
|
|---|
| 170 | \seepep{236}{Back to the \module{__future__}}{Written by Tim Peters,
|
|---|
| 171 | and primarily implemented by Jeremy Hylton.}
|
|---|
| 172 |
|
|---|
| 173 | \end{seealso}
|
|---|
| 174 |
|
|---|
| 175 | %======================================================================
|
|---|
| 176 | \section{PEP 207: Rich Comparisons}
|
|---|
| 177 |
|
|---|
| 178 | In earlier versions, Python's support for implementing comparisons on
|
|---|
| 179 | user-defined classes and extension types was quite simple. Classes
|
|---|
| 180 | could implement a \method{__cmp__} method that was given two instances
|
|---|
| 181 | of a class, and could only return 0 if they were equal or +1 or -1 if
|
|---|
| 182 | they weren't; the method couldn't raise an exception or return
|
|---|
| 183 | anything other than a Boolean value. Users of Numeric Python often
|
|---|
| 184 | found this model too weak and restrictive, because in the
|
|---|
| 185 | number-crunching programs that numeric Python is used for, it would be
|
|---|
| 186 | more useful to be able to perform elementwise comparisons of two
|
|---|
| 187 | matrices, returning a matrix containing the results of a given
|
|---|
| 188 | comparison for each element. If the two matrices are of different
|
|---|
| 189 | sizes, then the compare has to be able to raise an exception to signal
|
|---|
| 190 | the error.
|
|---|
| 191 |
|
|---|
| 192 | In Python 2.1, rich comparisons were added in order to support this
|
|---|
| 193 | need. Python classes can now individually overload each of the
|
|---|
| 194 | \code{<}, \code{<=}, \code{>}, \code{>=}, \code{==}, and \code{!=}
|
|---|
| 195 | operations. The new magic method names are:
|
|---|
| 196 |
|
|---|
| 197 | \begin{tableii}{c|l}{code}{Operation}{Method name}
|
|---|
| 198 | \lineii{<}{\method{__lt__}} \lineii{<=}{\method{__le__}}
|
|---|
| 199 | \lineii{>}{\method{__gt__}} \lineii{>=}{\method{__ge__}}
|
|---|
| 200 | \lineii{==}{\method{__eq__}} \lineii{!=}{\method{__ne__}}
|
|---|
| 201 | \end{tableii}
|
|---|
| 202 |
|
|---|
| 203 | (The magic methods are named after the corresponding Fortran operators
|
|---|
| 204 | \code{.LT.}. \code{.LE.}, \&c. Numeric programmers are almost
|
|---|
| 205 | certainly quite familiar with these names and will find them easy to
|
|---|
| 206 | remember.)
|
|---|
| 207 |
|
|---|
| 208 | Each of these magic methods is of the form \code{\var{method}(self,
|
|---|
| 209 | other)}, where \code{self} will be the object on the left-hand side of
|
|---|
| 210 | the operator, while \code{other} will be the object on the right-hand
|
|---|
| 211 | side. For example, the expression \code{A < B} will cause
|
|---|
| 212 | \code{A.__lt__(B)} to be called.
|
|---|
| 213 |
|
|---|
| 214 | Each of these magic methods can return anything at all: a Boolean, a
|
|---|
| 215 | matrix, a list, or any other Python object. Alternatively they can
|
|---|
| 216 | raise an exception if the comparison is impossible, inconsistent, or
|
|---|
| 217 | otherwise meaningless.
|
|---|
| 218 |
|
|---|
| 219 | The built-in \function{cmp(A,B)} function can use the rich comparison
|
|---|
|
|---|