Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 1 | \documentclass{howto} |
| 2 | |
| 3 | % $Id$ |
| 4 | |
| 5 | \title{What's New in Python 2.1} |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 6 | \release{0.05} |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 7 | \author{A.M. Kuchling} |
| 8 | \authoraddress{\email{amk1@bigfoot.com}} |
| 9 | \begin{document} |
| 10 | \maketitle\tableofcontents |
| 11 | |
| 12 | \section{Introduction} |
| 13 | |
Andrew M. Kuchling | f33c118 | 2001-01-23 02:48:26 +0000 | [diff] [blame] | 14 | {\large This document is a draft, and is subject to change until |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 15 | the final version of Python 2.1 is released. Currently it is up to date |
| 16 | for Python 2.1 alpha 2. Please send any comments, bug reports, or |
| 17 | questions, no matter how minor, to \email{amk1@bigfoot.com}. } |
Andrew M. Kuchling | f33c118 | 2001-01-23 02:48:26 +0000 | [diff] [blame] | 18 | |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 19 | It's that time again... time for a new Python release, version 2.1. |
| 20 | One recent goal of the Python development team has been to accelerate |
| 21 | the pace of new releases, with a new release coming every 6 to 9 |
| 22 | months. 2.1 is the first release to come out at this faster pace, with |
| 23 | the first alpha appearing in January, 3 months after the final version |
| 24 | of 2.0 was released. |
| 25 | |
| 26 | This article explains the new features in 2.1. While there aren't as |
| 27 | many changes in 2.1 as there were in Python 2.0, there are still some |
| 28 | pleasant surprises in store. 2.1 is the first release to be steered |
| 29 | through the use of Python Enhancement Proposals, or PEPs, so most of |
| 30 | the sizable changes have accompanying PEPs that provide more complete |
| 31 | documentation and a design rationale for the change. This article |
| 32 | doesn't attempt to document the new features completely, but simply |
| 33 | provides an overview of the new features for Python programmers. |
| 34 | Refer to the Python 2.1 documentation, or to the specific PEP, for |
| 35 | more details about any new feature that particularly interests you. |
| 36 | |
| 37 | Currently 2.1 is available in an alpha release, but the release |
| 38 | schedule calls for a beta release by late February 2001, and a final |
| 39 | release in April 2001. |
| 40 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 41 | %====================================================================== |
| 42 | \section{PEP 227: Nested Scopes} |
| 43 | |
| 44 | The largest change in Python 2.1 is to Python's scoping rules. In |
| 45 | Python 2.0, at any given time there are at most three namespaces used |
| 46 | to look up variable names: local, module-level, and the built-in |
| 47 | namespace. This often surprised people because it didn't match their |
| 48 | intuitive expectations. For example, a nested recursive function |
| 49 | definition doesn't work: |
| 50 | |
| 51 | \begin{verbatim} |
| 52 | def f(): |
| 53 | ... |
| 54 | def g(value): |
| 55 | ... |
| 56 | return g(value-1) + 1 |
| 57 | ... |
| 58 | \end{verbatim} |
| 59 | |
| 60 | The function \function{g()} will always raise a \exception{NameError} |
| 61 | exception, because the binding of the name \samp{g} isn't in either |
| 62 | its local namespace or in the module-level namespace. This isn't much |
| 63 | of a problem in practice (how often do you recursively define interior |
| 64 | functions like this?), but this also made using the \keyword{lambda} |
| 65 | statement clumsier, and this was a problem in practice. In code which |
| 66 | uses \keyword{lambda} you can often find local variables being copied |
| 67 | by passing them as the default values of arguments. |
| 68 | |
| 69 | \begin{verbatim} |
| 70 | def find(self, name): |
| 71 | "Return list of any entries equal to 'name'" |
| 72 | L = filter(lambda x, name=name: x == name, |
| 73 | self.list_attribute) |
| 74 | return L |
| 75 | \end{verbatim} |
| 76 | |
| 77 | The readability of Python code written in a strongly functional style |
| 78 | suffers greatly as a result. |
| 79 | |
| 80 | The most significant change to Python 2.1 is that static scoping has |
| 81 | been added to the language to fix this problem. As a first effect, |
| 82 | the \code{name=name} default argument is now unnecessary in the above |
| 83 | example. Put simply, when a given variable name is not assigned a |
| 84 | value within a function (by an assignment, or the \keyword{def}, |
| 85 | \keyword{class}, or \keyword{import} statements), references to the |
| 86 | variable will be looked up in the local namespace of the enclosing |
| 87 | scope. A more detailed explanation of the rules, and a dissection of |
| 88 | the implementation, can be found in the PEP. |
| 89 | |
| 90 | This change may cause some compatibility problems for code where the |
| 91 | same variable name is used both at the module level and as a local |
| 92 | variable within a function that contains further function definitions. |
| 93 | This seems rather unlikely though, since such code would have been |
| 94 | pretty confusing to read in the first place. |
| 95 | |
| 96 | One side effect of the change is that the statement from \code{from |
| 97 | \var{module} import *} has been made illegal inside a function scope. |
| 98 | The Python reference manual has said all along that \code{from |
| 99 | \var{module} import *} is only legal at the top level of a module, but |
| 100 | the CPython interpreter has never enforced this before; it will be |
| 101 | enforced in 2.1, though it's not yet clear if it will be a syntax |
| 102 | error or just a warning. In the alpha 2 release, it triggers a |
| 103 | \exception{SyntaxError} exception, but this check might be made more |
| 104 | lenient in following releases. |
| 105 | % XXX update the previous sentence for 2.1final |
| 106 | |
| 107 | \begin{seealso} |
| 108 | |
| 109 | \seepep{227}{Statically Nested Scopes}{Written and implemented by |
| 110 | Jeremy Hylton.} |
| 111 | |
| 112 | \end{seealso} |
| 113 | |
| 114 | |
| 115 | %====================================================================== |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 116 | \section{PEP 232: Function Attributes} |
| 117 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 118 | In Python 2.1, functions can now have arbitrary information attached |
| 119 | to them. People were often using docstrings to hold information about |
| 120 | functions and methods, because the \code{__doc__} attribute was the |
| 121 | only way of attaching any information to a function. For example, in |
| 122 | the Zope Web application server, functions are marked as safe for |
| 123 | public access by having a docstring, and in John Aycock's SPARK |
| 124 | parsing framework, docstrings hold parts of the BNF grammar to be |
| 125 | parsed. This overloading is unfortunate, since docstrings are really |
| 126 | intended to hold a function's documentation, and it means you can't |
| 127 | properly document functions intended for private use in Zope. |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 128 | |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 129 | Attributes can now be set and retrieved on functions, using the |
| 130 | regular Python syntax: |
| 131 | |
| 132 | \begin{verbatim} |
| 133 | def f(): pass |
| 134 | |
| 135 | f.publish = 1 |
| 136 | f.secure = 1 |
| 137 | f.grammar = "A ::= B (C D)*" |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 138 | \end{verbatim} |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 139 | |
| 140 | The dictionary containing attributes can be accessed as |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 141 | \member{__dict__}. Unlike the \member{__dict__} attribute of class |
| 142 | instances, in functions you can actually assign a new dictionary to |
| 143 | \member{__dict__}, though the new value is restricted to a regular |
| 144 | Python dictionary; you can't be tricky and set it to a |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 145 | \class{UserDict} instance, a DBM file, or any other random mapping |
| 146 | object. |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 147 | |
Andrew M. Kuchling | f228fd1 | 2001-01-22 17:52:19 +0000 | [diff] [blame] | 148 | \begin{seealso} |
| 149 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 150 | \seepep{232}{Function Attributes}{Written and implemented by Barry |
| 151 | Warsaw.} |
Andrew M. Kuchling | f228fd1 | 2001-01-22 17:52:19 +0000 | [diff] [blame] | 152 | |
| 153 | \end{seealso} |
| 154 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 155 | %====================================================================== |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 156 | \section{PEP 207: Rich Comparisons} |
| 157 | |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 158 | In earlier versions, Python's support for implementing comparisons on |
| 159 | user-defined classes and extension types was quite simple. Classes |
| 160 | could implement a \method{__cmp__} method that was given two instances |
| 161 | of a class, and could only return 0 if they were equal or +1 or -1 if |
| 162 | they weren't; the method couldn't raise an exception or return |
| 163 | anything other than a Boolean value. Users of Numeric Python often |
| 164 | found this model too weak and restrictive, because in the |
| 165 | number-crunching programs that numeric Python is used for, it would be |
| 166 | more useful to be able to perform elementwise comparisons of two |
| 167 | matrices, returning a matrix containing the results of a given |
| 168 | comparison for each element. If the two matrices are of different |
| 169 | sizes, then the compare has to be able to raise an exception to signal |
| 170 | the error. |
| 171 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 172 | In Python 2.1, rich comparisons were added in order to support this |
| 173 | need. Python classes can now individually overload each of the |
| 174 | \code{<}, \code{<=}, \code{>}, \code{>=}, \code{==}, and \code{!=} |
| 175 | operations. The new magic method names are: |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 176 | |
| 177 | \begin{tableii}{c|l}{code}{Operation}{Method name} |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 178 | \lineii{<}{\method{__lt__}} \lineii{<=}{\method{__le__}} |
| 179 | \lineii{>}{\method{__gt__}} \lineii{>=}{\method{__ge__}} |
| 180 | \lineii{==}{\method{__eq__}} \lineii{!=}{\method{__ne__}} |
| 181 | \end{tableii} |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 182 | |
| 183 | (The magic methods are named after the corresponding Fortran operators |
| 184 | \code{.LT.}. \code{.LE.}, \&c. Numeric programmers are almost |
| 185 | certainly quite familar with these names and will find them easy to |
| 186 | remember.) |
| 187 | |
| 188 | Each of these magic methods is of the form \code{\var{method}(self, |
| 189 | other)}, where \code{self} will be the object on the left-hand side of |
| 190 | the operator, while \code{other} will be the object on the right-hand |
| 191 | side. For example, the expression \code{A < B} will cause |
| 192 | \code{A.__lt__(B)} to be called. |
| 193 | |
| 194 | Each of these magic methods can return anything at all: a Boolean, a |
| 195 | matrix, a list, or any other Python object. Alternatively they can |
| 196 | raise an exception if the comparison is impossible, inconsistent, or |
| 197 | otherwise meaningless. |
| 198 | |
| 199 | The built-in \function{cmp(A,B)} function can use the rich comparison |
| 200 | machinery, and now accepts an optional argument specifying which |
| 201 | comparison operation to use; this is given as one of the strings |
| 202 | \code{"<"}, \code{"<="}, \code{">"}, \code{">="}, \code{"=="}, or |
| 203 | \code{"!="}. If called without the optional third argument, |
| 204 | \function{cmp()} will only return -1, 0, or +1 as in previous versions |
| 205 | of Python; otherwise it will call the appropriate method and can |
| 206 | return any Python object. |
| 207 | |
| 208 | There are also corresponding changes of interest to C programmers; |
| 209 | there's a new slot \code{tp_richcmp} in type objects and an API for |
| 210 | performing a given rich comparison. I won't cover the C API here, but |
| 211 | will refer you to PEP 207, or the documentation for Python's C API, |
| 212 | for the full list of related functions. |
| 213 | |
| 214 | \begin{seealso} |
| 215 | |
| 216 | \seepep{207}{Rich Comparisions}{Written by Guido van Rossum, heavily |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 217 | based on earlier work by David Ascher, and implemented by Guido van |
| 218 | Rossum.} |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 219 | |
| 220 | \end{seealso} |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 221 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 222 | %====================================================================== |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 223 | \section{PEP 230: Warning Framework} |
| 224 | |
| 225 | Over its 10 years of existence, Python has accumulated a certain |
| 226 | number of obsolete modules and features along the way. It's difficult |
| 227 | to know when a feature is safe to remove, since there's no way of |
Andrew M. Kuchling | f33c118 | 2001-01-23 02:48:26 +0000 | [diff] [blame] | 228 | knowing how much code uses it --- perhaps no programs depend on the |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 229 | feature, or perhaps many do. To enable removing old features in a |
| 230 | more structured way, a warning framework was added. When the Python |
| 231 | developers want to get rid of a feature, it will first trigger a |
| 232 | warning in the next version of Python. The following Python version |
| 233 | can then drop the feature, and users will have had a full release |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 234 | cycle to remove uses of the old feature. |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 235 | |
| 236 | Python 2.1 adds the warning framework to be used in this scheme. It |
| 237 | adds a \module{warnings} module that provide functions to issue |
| 238 | warnings, and to filter out warnings that you don't want to be |
| 239 | displayed. Third-party modules can also use this framework to |
| 240 | deprecate old features that they no longer wish to support. |
| 241 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 242 | For example, in Python 2.1 the \module{regex} module is deprecated, so |
| 243 | importing it causes a warning to be printed: |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 244 | |
| 245 | \begin{verbatim} |
| 246 | >>> import regex |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 247 | __main__:1: DeprecationWarning: the regex module |
| 248 | is deprecated; please use the re module |
| 249 | >>> |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 250 | \end{verbatim} |
| 251 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 252 | Warnings can be issued by calling the \function{warnings.warn} |
| 253 | function: |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 254 | |
| 255 | \begin{verbatim} |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 256 | warnings.warn("feature X no longer supported") |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 257 | \end{verbatim} |
| 258 | |
| 259 | The first parameter is the warning message; an additional optional |
| 260 | parameters can be used to specify a particular warning category. |
| 261 | |
| 262 | Filters can be added to disable certain warnings; a regular expression |
| 263 | pattern can be applied to the message or to the module name in order |
| 264 | to suppress a warning. For example, you may have a program that uses |
| 265 | the \module{regex} module and not want to spare the time to convert it |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 266 | to use the \module{re} module right now. The warning can be |
| 267 | suppressed by calling |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 268 | |
| 269 | \begin{verbatim} |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 270 | import warnings |
| 271 | warnings.filterwarnings(action = 'ignore', |
| 272 | message='.*regex module is deprecated', |
| 273 | category=DeprecationWarning, |
| 274 | module = '__main__') |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 275 | \end{verbatim} |
| 276 | |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 277 | This adds a filter that will apply only to warnings of the class |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 278 | \class{DeprecationWarning} triggered in the \module{__main__} module, |
| 279 | and applies a regular expression to only match the message about the |
| 280 | \module{regex} module being deprecated, and will cause such warnings |
| 281 | to be ignored. Warnings can also be printed only once, printed every |
| 282 | time the offending code is executed, or turned into exceptions that |
| 283 | will cause the program to stop (unless the exceptions are caught in |
| 284 | the usual way, of course). |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 285 | |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 286 | Functions were also added to Python's C API for issuing warnings; |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 287 | refer to PEP 230 or to Python's API documentation for the details. |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 288 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 289 | \begin{seealso} |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 290 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 291 | \seepep{5}{Guidelines for Language Evolution}{Written |
| 292 | by Paul Prescod, to specify procedures to be followed when removing |
| 293 | old features from Python. The policy described in this PEP hasn't |
| 294 | been officially adopted, but the eventual policy probably won't be too |
| 295 | different from Prescod's proposal.} |
| 296 | |
| 297 | \seepep{230}{Warning Framework}{Written and implemented by Guido van |
| 298 | Rossum.} |
| 299 | |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 300 | \end{seealso} |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 301 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 302 | %====================================================================== |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 303 | \section{PEP 229: New Build System} |
| 304 | |
| 305 | When compiling Python, the user had to go in and edit the |
| 306 | \file{Modules/Setup} file in order to enable various additional |
| 307 | modules; the default set is relatively small and limited to modules |
| 308 | that compile on most Unix platforms. This means that on Unix |
| 309 | platforms with many more features, most notably Linux, Python |
| 310 | installations often don't contain all useful modules they could. |
| 311 | |
| 312 | Python 2.0 added the Distutils, a set of modules for distributing and |
| 313 | installing extensions. In Python 2.1, the Distutils are used to |
| 314 | compile much of the standard library of extension modules, |
Andrew M. Kuchling | f33c118 | 2001-01-23 02:48:26 +0000 | [diff] [blame] | 315 | autodetecting which ones are supported on the current machine. It's |
| 316 | hoped that this will make Python installations easier and more |
| 317 | featureful. |
| 318 | |
| 319 | Instead of having to edit the \file{Modules/Setup} file in order to |
| 320 | enable modules, a \file{setup.py} script in the top directory of the |
| 321 | Python source distribution is run at build time, and attempts to |
| 322 | discover which modules can be enabled by examining the modules and |
| 323 | header files on the system. In 2.1alpha1, there's very little you can |
| 324 | do to change \file{setup.py}'s behaviour, or to discover why a given |
| 325 | module isn't compiled. If you run into problems in 2.1alpha1, please |
| 326 | report them, and be prepared to dive into \file{setup.py} in order to |
| 327 | fix autodetection of a given library on your system. In the alpha2 |
| 328 | release I plan to add ways to have more control over what the script |
| 329 | does (probably command-line arguments to \file{configure} or to |
| 330 | \file{setup.py}). |
| 331 | |
| 332 | If it turns out to be impossible to make autodetection work reliably, |
| 333 | it's possible that this change may become an optional build method |
| 334 | instead of the default, or it may even be backed out completely. |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 335 | |
Andrew M. Kuchling | 4308d3c | 2001-01-29 17:36:53 +0000 | [diff] [blame] | 336 | In another far-reaching change to the build mechanism, Neil |
| 337 | Schemenauer restructured things so Python now uses a single makefile |
| 338 | that isn't recursive, instead of makefiles in the top directory and in |
| 339 | each of the Python/, Parser/, Objects/, and Modules/ subdirectories. |
| 340 | This makes building Python faster, and also makes the build process |
| 341 | clearer and simpler. |
| 342 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 343 | \begin{seealso} |
| 344 | |
| 345 | \seepep{229}{Using Distutils to Build Python}{Written |
| 346 | and implemented by A.M. Kuchling.} |
| 347 | |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 348 | \end{seealso} |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 349 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 350 | %====================================================================== |
| 351 | \section{Weak References} |
| 352 | |
| 353 | Weak references are a minor but useful new data type in the Python |
| 354 | programmer's toolbox. Storing a reference to an object (say, in a |
| 355 | dictionary or a list) has the side effect of keeping that object alive |
| 356 | forever. There are a few specific cases where this behaviour is |
| 357 | undesirable, object caches being the most common one, and another |
| 358 | being circular references in data structures such as trees. |
| 359 | |
| 360 | For example, a tree might be implemented as a set of \class{Node} |
| 361 | instances where each instances contains a list of its children. If |
| 362 | you need to be able to determine the parent of a given \class{Node}, |
| 363 | an obvious solution would be to have each instance have a reference to |
| 364 | its parent. This creates lots of circular references. |
| 365 | |
| 366 | XXX finish the rest of this section |
| 367 | |
| 368 | \begin{seealso} |
| 369 | |
| 370 | \seepep{205}{Weak References}{Written and implemented by |
| 371 | Fred~L. Drake,~Jr.} |
| 372 | |
| 373 | \end{seealso} |
| 374 | |
| 375 | %====================================================================== |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 376 | \section{PEP 217: Interactive Display Hook} |
| 377 | |
| 378 | When using the Python interpreter interactively, the output of |
| 379 | commands is displayed using the built-in \function{repr()} function. |
| 380 | In Python 2.1, the variable \module{sys.displayhook} can be set to a |
| 381 | callable object which will be called instead of \function{repr()}. |
| 382 | For example, you can set it to a special pretty-printing function: |
| 383 | |
| 384 | \begin{verbatim} |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 385 | >>> # Create a recursive data structure |
| 386 | ... L = [1,2,3] |
| 387 | >>> L.append(L) |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 388 | >>> L # Show Python's default output |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 389 | [1, 2, 3, [...]] |
| 390 | >>> # Use pprint.pprint() as the display function |
| 391 | ... import sys, pprint |
| 392 | >>> sys.displayhook = pprint.pprint |
| 393 | >>> L |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 394 | [1, 2, 3, <Recursion on list with id=135143996>] |
| 395 | >>> |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 396 | \end{verbatim} |
| 397 | |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 398 | \begin{seealso} |
| 399 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 400 | \seepep{217}{Display Hook for Interactive Use}{Written and implemented |
| 401 | by Moshe Zadka.} |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 402 | |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 403 | \end{seealso} |
| 404 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 405 | %====================================================================== |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 406 | \section{PEP 208: New Coercion Model} |
| 407 | |
| 408 | How numeric coercion is done at the C level was significantly |
| 409 | modified. This will only affect the authors of C extensions to |
| 410 | Python, allowing them more flexibility in writing extension types that |
| 411 | support numeric operations. |
| 412 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 413 | Extension types can now set the type flag \code{Py_TPFLAGS_CHECKTYPES} |
| 414 | in their \code{PyTypeObject} structure to indicate that they support |
| 415 | the new coercion model. In such extension types, the numeric slot |
| 416 | functions can no longer assume that they'll be passed two arguments of |
| 417 | the same type; instead they may be passed two arguments of differing |
| 418 | types, and can then perform their own internal coercion. If the slot |
| 419 | function is passed a type it can't handle, it can indicate the failure |
| 420 | by returning a reference to the \code{Py_NotImplemented} singleton |
| 421 | value. The numeric functions of the other type will then be tried, |
| 422 | and perhaps they can handle the operation; if the other type also |
| 423 | returns \code{Py_NotImplemented}, then a \exception{TypeError} will be |
| 424 | raised. Numeric methods written in Python can also return |
| 425 | \code{Py_NotImplemented}, causing the interpreter to act as if the |
| 426 | method did not exist (perhaps raising a \exception{TypeError}, perhaps |
| 427 | trying another object's numeric methods). |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 428 | |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 429 | \begin{seealso} |
| 430 | |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 431 | \seepep{208}{Reworking the Coercion Model}{Written and implemented by |
| 432 | Neil Schemenauer, heavily based upon earlier work by Marc-Andr\'e |
| 433 | Lemburg. Read this to understand the fine points of how numeric |
| 434 | operations will now be processed at the C level.} |
| 435 | |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 436 | \end{seealso} |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 437 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 438 | %====================================================================== |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 439 | \section{Minor Changes and Fixes} |
| 440 | |
| 441 | There were relatively few smaller changes made in Python 2.1 due to |
| 442 | the shorter release cycle. A search through the CVS change logs turns |
| 443 | up 57 patches applied, and 86 bugs fixed; both figures are likely to |
| 444 | be underestimates. Some of the more notable changes are: |
| 445 | |
| 446 | \begin{itemize} |
| 447 | |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 448 | \item The speed of line-oriented file I/O has been improved because |
| 449 | people often complain about its lack of speed, and because it's often |
| 450 | been used as a na\"ive benchmark. The \method{readline()} method of |
| 451 | file objects has therefore been rewritten to be much faster. The |
| 452 | exact amount of the speedup will vary from platform to platform |
| 453 | depending on how slow the C library's \function{getc()} was, but is |
| 454 | around 66\%, and potentially much faster on some particular operating |
Andrew M. Kuchling | f228fd1 | 2001-01-22 17:52:19 +0000 | [diff] [blame] | 455 | systems. Tim Peters did much of the benchmarking and coding for this |
| 456 | change, motivated by a discussion in comp.lang.python. |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 457 | |
| 458 | A new module and method for file objects was also added, contributed |
| 459 | by Jeff Epler. The new method, \method{xreadlines()}, is similar to |
| 460 | the existing \function{xrange()} built-in. \function{xreadlines()} |
| 461 | returns an opaque sequence object that only supports being iterated |
Andrew M. Kuchling | f228fd1 | 2001-01-22 17:52:19 +0000 | [diff] [blame] | 462 | over, reading a line on every iteration but not reading the entire |
Andrew M. Kuchling | f33c118 | 2001-01-23 02:48:26 +0000 | [diff] [blame] | 463 | file into memory as the existing \method{readlines()} method does. |
| 464 | You'd use it like this: |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 465 | |
| 466 | \begin{verbatim} |
| 467 | for line in sys.stdin.xreadlines(): |
| 468 | # ... do something for each line ... |
| 469 | ... |
| 470 | \end{verbatim} |
| 471 | |
Andrew M. Kuchling | f228fd1 | 2001-01-22 17:52:19 +0000 | [diff] [blame] | 472 | For a fuller discussion of the line I/O changes, see the python-dev |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 473 | summary for January 1-15, 2001. |
Andrew M. Kuchling | 91834c6 | 2001-01-22 19:51:13 +0000 | [diff] [blame] | 474 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 475 | \item A new method, \method{popitem()}, was added to dictionaries to |
| 476 | enable destructively iterating through the contents of a dictionary; |
| 477 | this can be faster for large dictionaries because . |
| 478 | \code{D.popitem()} removes a random \code{(\var{key}, \var{value})} |
| 479 | pair from the dictionary and returns it as a 2-tuple. This was |
| 480 | implemented mostly by Tim Peters and Guido van Rossum, after a |
| 481 | suggestion and preliminary patch by Moshe Zadka. |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 482 | |
| 483 | \item \module{curses.panel}, a wrapper for the panel library, part of |
| 484 | ncurses and of SYSV curses, was contributed by Thomas Gellekum. The |
| 485 | panel library provides windows with the additional feature of depth. |
| 486 | Windows can be moved higher or lower in the depth ordering, and the |
| 487 | panel library figures out where panels overlap and which sections are |
| 488 | visible. |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 489 | |
| 490 | \item Modules can now control which names are imported when \code{from |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 491 | \var{module} import *} is used, by defining an \code{__all__} |
| 492 | attribute containing a list of names that will be imported. One |
| 493 | common complaint is that if the module imports other modules such as |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 494 | \module{sys} or \module{string}, \code{from \var{module} import *} |
| 495 | will add them to the importing module's namespace. To fix this, |
| 496 | simply list the public names in \code{__all__}: |
| 497 | |
| 498 | \begin{verbatim} |
| 499 | # List public names |
| 500 | __all__ = ['Database', 'open'] |
| 501 | \end{verbatim} |
| 502 | |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 503 | A stricter version of this patch was first suggested and implemented |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 504 | by Ben Wolfson, but after some python-dev discussion, a weaker final |
| 505 | version was checked in. |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 506 | |
Andrew M. Kuchling | 91834c6 | 2001-01-22 19:51:13 +0000 | [diff] [blame] | 507 | \item The PyXML package has gone through a few releases since Python |
| 508 | 2.0, and Python 2.1 includes an updated version of the \module{xml} |
| 509 | package. Some of the noteworthy changes include support for Expat |
| 510 | 1.2, the ability for Expat parsers to handle files in any encoding |
| 511 | supported by Python, and various bugfixes for SAX, DOM, and the |
| 512 | \module{minidom} module. |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 513 | |
Andrew M. Kuchling | f228fd1 | 2001-01-22 17:52:19 +0000 | [diff] [blame] | 514 | \item Various functions in the \module{time} module, such as |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 515 | \function{asctime()} and \function{localtime()}, require a floating |
| 516 | point argument containing the time in seconds since the epoch. The |
| 517 | most common use of these functions is to work with the current time, |
| 518 | so the floating point argument has been made optional; when a value |
| 519 | isn't provided, the current time will be used. For example, log file |
| 520 | entries usually need a string containing the current time; in Python |
| 521 | 2.1, \code{time.asctime()} can be used, instead of the lengthier |
| 522 | \code{time.asctime(time.localtime(time.time()))} that was previously |
| 523 | required. |
Andrew M. Kuchling | f228fd1 | 2001-01-22 17:52:19 +0000 | [diff] [blame] | 524 | |
| 525 | This change was proposed and implemented by Thomas Wouters. |
| 526 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 527 | \item Applying \function{repr()} to strings previously used octal |
| 528 | escapes for non-printable characters; for example, a newline was |
| 529 | \code{'\e 012'}. This was a vestigial trace of Python's C ancestry, but |
| 530 | today octal is of very little practical use. Ka-Ping Yee suggested |
| 531 | using hex escapes instead of octal ones, and using the \code{\e n}, |
| 532 | \code{\e t}, \code{\e r} escapes for the appropriate characters, and |
| 533 | implemented this new formatting. |
Andrew M. Kuchling | 4308d3c | 2001-01-29 17:36:53 +0000 | [diff] [blame] | 534 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 535 | \item The \module{ftplib} module now defaults to retrieving files in |
| 536 | passive mode, because passive mode is more likely to work from behind |
| 537 | a firewall. This request came from the Debian bug tracking system, |
| 538 | since other Debian packages use \module{ftplib} to retrieve files and |
| 539 | then don't work from behind a firewall. It's deemed unlikely that |
| 540 | this will cause problems for anyone, because Netscape defaults to |
| 541 | passive mode and few people complain, but if passive mode is |
| 542 | unsuitable for your application or network setup, call |
| 543 | \method{set_pasv(0)} on FTP objects to disable passive mode. |
Andrew M. Kuchling | 91834c6 | 2001-01-22 19:51:13 +0000 | [diff] [blame] | 544 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 545 | \item Support for raw socket access has been added to the |
| 546 | \module{socket} module, contributed by Grant Edwards. |
| 547 | |
| 548 | \item Syntax errors detected at compile-time can now raise exceptions |
| 549 | containing the filename and line number of the error, a pleasant side |
| 550 | effect of the compiler reorganization done by Jeremy Hylton. |
| 551 | |
| 552 | \item The size of the Unicode character database was shrunk by another |
| 553 | 340K thanks to Fredrik Lundh. |
Andrew M. Kuchling | 91834c6 | 2001-01-22 19:51:13 +0000 | [diff] [blame] | 554 | |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 555 | \end{itemize} |
| 556 | |
| 557 | And there's the usual list of bugfixes, minor memory leaks, docstring |
| 558 | edits, and other tweaks, too lengthy to be worth itemizing; see the |
| 559 | CVS logs for the full details if you want them. |
| 560 | |
| 561 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 562 | %====================================================================== |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 563 | \section{Acknowledgements} |
| 564 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame^] | 565 | The author would like to thank the following people for offering |
| 566 | suggestions on various drafts of this article: Graeme Cross, David |
| 567 | Goodger, Jay Graves, Michael Hudson, Marc-Andr\'e Lemburg, Fredrik |
| 568 | Lundh, Neil Schemenauer, Thomas Wouters. |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 569 | |
| 570 | \end{document} |