Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 1 | \documentclass{howto} |
| 2 | |
| 3 | % $Id$ |
| 4 | |
| 5 | \title{What's New in Python 2.2} |
Andrew M. Kuchling | 7aa63c2 | 2001-10-30 14:35:03 +0000 | [diff] [blame^] | 6 | \release{0.08} |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 7 | \author{A.M. Kuchling} |
Andrew M. Kuchling | 7bf8277 | 2001-07-11 18:54:26 +0000 | [diff] [blame] | 8 | \authoraddress{\email{akuchlin@mems-exchange.org}} |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 9 | \begin{document} |
| 10 | \maketitle\tableofcontents |
| 11 | |
| 12 | \section{Introduction} |
| 13 | |
| 14 | {\large This document is a draft, and is subject to change until the |
Andrew M. Kuchling | 9e9c135 | 2001-08-11 03:06:50 +0000 | [diff] [blame] | 15 | final version of Python 2.2 is released. Currently it's up to date |
Andrew M. Kuchling | b83769c | 2001-10-26 20:07:03 +0000 | [diff] [blame] | 16 | for Python 2.2 beta 1. Please send any comments, bug reports, or |
Andrew M. Kuchling | 9e9c135 | 2001-08-11 03:06:50 +0000 | [diff] [blame] | 17 | questions, no matter how minor, to \email{akuchlin@mems-exchange.org}. |
| 18 | } |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 19 | |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 20 | This article explains the new features in Python 2.2. |
| 21 | |
| 22 | Python 2.2 can be thought of as the "cleanup release". There are some |
| 23 | features such as generators and iterators that are completely new, but |
| 24 | most of the changes, significant and far-reaching though they may be, |
| 25 | are aimed at cleaning up irregularities and dark corners of the |
| 26 | language design. |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 27 | |
Andrew M. Kuchling | 1497b62 | 2001-09-24 14:51:16 +0000 | [diff] [blame] | 28 | This article doesn't attempt to provide a complete specification of |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 29 | the new features, but instead provides a convenient overview. For |
| 30 | full details, you should refer to the documentation for Python 2.2, |
Fred Drake | 0d00254 | 2001-07-17 13:55:33 +0000 | [diff] [blame] | 31 | such as the |
| 32 | \citetitle[http://python.sourceforge.net/devel-docs/lib/lib.html]{Python |
| 33 | Library Reference} and the |
| 34 | \citetitle[http://python.sourceforge.net/devel-docs/ref/ref.html]{Python |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 35 | Reference Manual}. |
| 36 | % XXX These \citetitle marks should get the python.org URLs for the final |
Fred Drake | 0d00254 | 2001-07-17 13:55:33 +0000 | [diff] [blame] | 37 | % release, just as soon as the docs are published there. |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 38 | If you want to understand the complete implementation and design |
| 39 | rationale for a change, refer to the PEP for a particular new feature. |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 40 | |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 41 | The final release of Python 2.2 is planned for December 2001. |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 42 | |
Andrew M. Kuchling | 1497b62 | 2001-09-24 14:51:16 +0000 | [diff] [blame] | 43 | \begin{seealso} |
| 44 | |
| 45 | \url{http://www.unixreview.com/documents/s=1356/urm0109h/0109h.htm} |
| 46 | {``What's So Special About Python 2.2?'' is also about the new 2.2 |
| 47 | features, and was written by Cameron Laird and Kathryn Soraiz.} |
| 48 | |
| 49 | \end{seealso} |
| 50 | |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 51 | |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 52 | %====================================================================== |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 53 | \section{PEP 252: Type and Class Changes} |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 54 | |
Andrew M. Kuchling | 279e744 | 2001-10-22 02:03:40 +0000 | [diff] [blame] | 55 | The largest and most far-reaching changes in Python 2.2 are to |
| 56 | Python's model of objects and classes. The changes should be backward |
| 57 | compatible, so it's likely that your code will continue to run |
| 58 | unchanged, but the changes provide some amazing new capabilities. |
| 59 | Before beginning this, the longest and most complicated section of |
| 60 | this article, I'll provide an overview of the changes and offer some |
| 61 | comments. |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 62 | |
Andrew M. Kuchling | 279e744 | 2001-10-22 02:03:40 +0000 | [diff] [blame] | 63 | A long time ago I wrote a Web page |
| 64 | (\url{http://www.amk.ca/python/writing/warts.html}) listing flaws in |
| 65 | Python's design. One of the most significant flaws was that it's |
| 66 | impossible to subclass Python types implemented in C. In particular, |
| 67 | it's not possible to subclass built-in types, so you can't just |
| 68 | subclass, say, lists in order to add a single useful method to them. |
| 69 | The \module{UserList} module provides a class that supports all of the |
| 70 | methods of lists and that can be subclassed further, but there's lots |
| 71 | of C code that expects a regular Python list and won't accept a |
| 72 | \class{UserList} instance. |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 73 | |
Andrew M. Kuchling | 279e744 | 2001-10-22 02:03:40 +0000 | [diff] [blame] | 74 | Python 2.2 fixes this, and in the process adds some exciting new |
| 75 | capabilities. A brief summary: |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 76 | |
Andrew M. Kuchling | 279e744 | 2001-10-22 02:03:40 +0000 | [diff] [blame] | 77 | \begin{itemize} |
Andrew M. Kuchling | d6e40e2 | 2001-09-10 16:18:50 +0000 | [diff] [blame] | 78 | |
Andrew M. Kuchling | 279e744 | 2001-10-22 02:03:40 +0000 | [diff] [blame] | 79 | \item You can subclass built-in types such as lists and even integers, |
| 80 | and your subclasses should work in every place that requires the |
| 81 | original type. |
| 82 | |
| 83 | \item It's now possible to define static and class methods, in addition |
| 84 | to the instance methods available in previous versions of Python. |
| 85 | |
| 86 | \item It's also possible to automatically call methods on accessing or |
| 87 | setting an instance attribute by using a new mechanism called |
| 88 | \dfn{properties}. Many uses of \method{__getattr__} can be rewritten |
| 89 | to use properties instead, making the resulting code simpler and |
| 90 | faster. As a small side benefit, attributes can now have docstrings, |
| 91 | too. |
| 92 | |
| 93 | \item The list of legal attributes for an instance can be limited to a |
| 94 | particular set using \dfn{slots}, making it possible to safeguard |
| 95 | against typos and perhaps make more optimizations possible in future |
| 96 | versions of Python. |
| 97 | |
| 98 | \end{itemize} |
| 99 | |
| 100 | Some users have voiced concern about all these changes. Sure, they |
| 101 | say, the new features are neat and lend themselves to all sorts of |
| 102 | tricks that weren't possible in previous versions of Python, but |
| 103 | they also make the language more complicated. Some people have said |
| 104 | that they've always recommended Python for its simplicity, and feel |
| 105 | that its simplicity is being lost. |
| 106 | |
| 107 | Personally, I think there's no need to worry. Many of the new |
| 108 | features are quite esoteric, and you can write a lot of Python code |
| 109 | without ever needed to be aware of them. Writing a simple class is no |
| 110 | more difficult than it ever was, so you don't need to bother learning |
| 111 | or teaching them unless they're actually needed. Some very |
| 112 | complicated tasks that were previously only possible from C will now |
| 113 | be possible in pure Python, and to my mind that's all for the better. |
| 114 | |
| 115 | I'm not going to attempt to cover every single corner case and small |
| 116 | change that were required to make the new features work. Instead this |
Andrew M. Kuchling | cf31d5d | 2001-10-26 20:37:55 +0000 | [diff] [blame] | 117 | section will paint only the broad strokes. See section~\ref{sect-rellinks}, |
Andrew M. Kuchling | 279e744 | 2001-10-22 02:03:40 +0000 | [diff] [blame] | 118 | ``Related Links'', for further sources of information about Python 2.2's new |
| 119 | object model. |
| 120 | |
| 121 | |
| 122 | \subsection{Old and New Classes} |
| 123 | |
| 124 | First, you should know that Python 2.2 really has two kinds of |
| 125 | classes: classic or old-style classes, and new-style classes. The |
| 126 | old-style class model is exactly the same as the class model in |
| 127 | earlier versions of Python. All the new features described in this |
| 128 | section apply only to new-style classes. This divergence isn't |
| 129 | intended to last forever; eventually old-style classes will be |
| 130 | dropped, possibly in Python 3.0. |
| 131 | |
Andrew M. Kuchling | 4855b02 | 2001-10-23 20:26:16 +0000 | [diff] [blame] | 132 | So how do you define a new-style class? You do it by subclassing an |
| 133 | existing new-style class. Most of Python's built-in types, such as |
| 134 | integers, lists, dictionaries, and even files, are new-style classes |
| 135 | now. A new-style class named \class{object}, the base class for all |
| 136 | built-in types, has been also been added so if no built-in type is |
| 137 | suitable, you can just subclass \class{object}: |
| 138 | |
| 139 | \begin{verbatim} |
| 140 | class C(object): |
| 141 | def __init__ (self): |
| 142 | ... |
| 143 | ... |
| 144 | \end{verbatim} |
| 145 | |
| 146 | This means that \keyword{class} statements that don't have any base |
Andrew M. Kuchling | cf31d5d | 2001-10-26 20:37:55 +0000 | [diff] [blame] | 147 | classes are always classic classes in Python 2.2. (Actually you can |
| 148 | also change this by setting a module-level variable named |
| 149 | \member{__metaclass__} --- see \pep{253} for the details --- but it's |
| 150 | easier to just subclass \keyword{object}.) |
Andrew M. Kuchling | 4855b02 | 2001-10-23 20:26:16 +0000 | [diff] [blame] | 151 | |
| 152 | The type objects for the built-in types are available as built-ins, |
| 153 | named using a clever trick. Python has always had built-in functions |
| 154 | named \function{int()}, \function{float()}, and \function{str()}. In |
| 155 | 2.2, they aren't functions any more, but type objects that behave as |
| 156 | factories when called. |
| 157 | |
| 158 | \begin{verbatim} |
| 159 | >>> int |
| 160 | <type 'int'> |
| 161 | >>> int('123') |
| 162 | 123 |
| 163 | \end{verbatim} |
| 164 | |
| 165 | To make the set of types complete, new type objects such as |
Andrew M. Kuchling | 1117d93 | 2001-10-29 20:37:47 +0000 | [diff] [blame] | 166 | \function{dict} and \function{file} have been added. Here's a |
Andrew M. Kuchling | b83769c | 2001-10-26 20:07:03 +0000 | [diff] [blame] | 167 | more interesting example, adding a \method{lock()} method to file |
| 168 | objects: |
Andrew M. Kuchling | 4855b02 | 2001-10-23 20:26:16 +0000 | [diff] [blame] | 169 | |
| 170 | \begin{verbatim} |
Andrew M. Kuchling | b83769c | 2001-10-26 20:07:03 +0000 | [diff] [blame] | 171 | class LockableFile(file): |
| 172 | def lock (self, operation, length=0, start=0, whence=0): |
| 173 | import fcntl |
| 174 | return fcntl.lockf(self.fileno(), operation, |
| 175 | length, start, whence) |
Andrew M. Kuchling | 4855b02 | 2001-10-23 20:26:16 +0000 | [diff] [blame] | 176 | \end{verbatim} |
| 177 | |
Andrew M. Kuchling | b83769c | 2001-10-26 20:07:03 +0000 | [diff] [blame] | 178 | The now-obsolete \module{posixfile} module contained a class that |
| 179 | emulated all of a file object's methods and also added a |
| 180 | \method{lock()} method, but this class couldn't be passed to internal |
| 181 | functions that expected a built-in file, something which is possible |
| 182 | with our new \class{LockableFile}. |
Andrew M. Kuchling | 279e744 | 2001-10-22 02:03:40 +0000 | [diff] [blame] | 183 | |
| 184 | |
| 185 | \subsection{Descriptors} |
| 186 | |
| 187 | In previous versions of Python, there was no consistent way to |
| 188 | discover what attributes and methods were supported by an object. |
| 189 | There were some informal conventions, such as defining |
| 190 | \member{__members__} and \member{__methods__} attributes that were |
| 191 | lists of names, but often the author of an extension type or a class |
| 192 | wouldn't bother to define them. You could fall back on inspecting the |
| 193 | \member{__dict__} of an object, but when class inheritance or an |
| 194 | arbitrary \method{__getattr__} hook were in use this could still be |
| 195 | inaccurate. |
| 196 | |
| 197 | The one big idea underlying the new class model is that an API for |
| 198 | describing the attributes of an object using \dfn{descriptors} has |
| 199 | been formalized. Descriptors specify the value of an attribute, |
| 200 | stating whether it's a method or a field. With the descriptor API, |
| 201 | static methods and class methods become possible, as well as more |
| 202 | exotic constructs. |
| 203 | |
| 204 | Attribute descriptors are objects that live inside class objects, and |
| 205 | have a few attributes of their own: |
| 206 | |
| 207 | \begin{itemize} |
| 208 | |
| 209 | \item \member{__name__} is the attribute's name. |
| 210 | |
| 211 | \item \member{__doc__} is the attribute's docstring. |
| 212 | |
| 213 | \item \method{__get__(\var{object})} is a method that retrieves the attribute value from \var{object}. |
| 214 | |
Andrew M. Kuchling | 4f9e220 | 2001-10-29 18:09:42 +0000 | [diff] [blame] | 215 | \item \method{__set__(\var{object}, \var{value})} sets the attribute |
Andrew M. Kuchling | 279e744 | 2001-10-22 02:03:40 +0000 | [diff] [blame] | 216 | on \var{object} to \var{value}. |
| 217 | |
| 218 | \end{itemize} |
| 219 | |
| 220 | For example, when you write \code{obj.x}, the steps that Python |
| 221 | actually performs are: |
| 222 | |
| 223 | \begin{verbatim} |
| 224 | descriptor = obj.__class__.x |
Andrew M. Kuchling | 7cc13de | 2001-10-30 14:22:11 +0000 | [diff] [blame] | 225 | descriptor.__get__(obj) |
Andrew M. Kuchling | 279e744 | 2001-10-22 02:03:40 +0000 | [diff] [blame] | 226 | \end{verbatim} |
| 227 | |
Andrew M. Kuchling | 7cc13de | 2001-10-30 14:22:11 +0000 | [diff] [blame] | 228 | For methods, \method{descriptor.__get__} returns a temporary object that's |
Andrew M. Kuchling | 279e744 | 2001-10-22 02:03:40 +0000 | [diff] [blame] | 229 | callable, and wraps up the instance and the method to be called on it. |
| 230 | This is also why static methods and class methods are now possible; |
| 231 | they have descriptors that wrap up just the method, or the method and |
| 232 | the class. As a brief explanation of these new kinds of methods, |
| 233 | static methods aren't passed the instance, and therefore resemble |
| 234 | regular functions. Class methods are passed the class of the object, |
| 235 | but not the object itself. Static and class methods is defined like |
| 236 | this: |
| 237 | |
| 238 | \begin{verbatim} |
| 239 | class C: |
| 240 | def f(arg1, arg2): |
| 241 | ... |
| 242 | f = staticmethod(f) |
| 243 | |
| 244 | def g(cls, arg1, arg2): |
| 245 | ... |
| 246 | g = classmethod(g) |
| 247 | \end{verbatim} |
| 248 | |
| 249 | The \function{staticmethod()} function takes the function |
| 250 | \function{f}, and returns it wrapped up in a descriptor so it can be |
| 251 | stored in the class object. You might expect there to be special |
| 252 | syntax for creating such methods (\code{def static f()}, |
| 253 | \code{defstatic f()}, or something like that) but no such syntax has |
| 254 | been defined yet; that's been left for future versions. |
| 255 | |
| 256 | More new features, such as slots and properties, are also implemented |
| 257 | as new kinds of descriptors, and it's not difficult to write a |
| 258 | descriptor class that does something novel. For example, it would be |
| 259 | possible to write a descriptor class that made it possible to write |
| 260 | Eiffel-style preconditions and postconditions for a method. A class |
| 261 | that used this feature might be defined like this: |
| 262 | |
| 263 | \begin{verbatim} |
| 264 | from eiffel import eiffelmethod |
| 265 | |
| 266 | class C: |
| 267 | def f(self, arg1, arg2): |
| 268 | # The actual function |
| 269 | def pre_f(self): |
| 270 | # Check preconditions |
| 271 | def post_f(self): |
| 272 | # Check postconditions |
| 273 | |
| 274 | f = eiffelmethod(f, pre_f, post_f) |
| 275 | \end{verbatim} |
| 276 | |
| 277 | Note that a person using the new \function{eiffelmethod()} doesn't |
| 278 | have to understand anything about descriptors. This is why I think |
| 279 | the new features don't increase the basic complexity of the language. |
| 280 | There will be a few wizards who need to know about it in order to |
| 281 | write \function{eiffelmethod()} or the ZODB or whatever, but most |
| 282 | users will just write code on top of the resulting libraries and |
| 283 | ignore the implementation details. |
| 284 | |
Andrew M. Kuchling | 4855b02 | 2001-10-23 20:26:16 +0000 | [diff] [blame] | 285 | \subsection{Multiple Inheritance: The Diamond Rule} |
Andrew M. Kuchling | 279e744 | 2001-10-22 02:03:40 +0000 | [diff] [blame] | 286 | |
Andrew M. Kuchling | 4855b02 | 2001-10-23 20:26:16 +0000 | [diff] [blame] | 287 | Multiple inheritance has also been made more useful through changing |
| 288 | the rules under which names are resolved. Consider this set of classes |
| 289 | (diagram taken from \pep{253} by Guido van Rossum): |
| 290 | |
| 291 | \begin{verbatim} |
| 292 | class A: |
| 293 | ^ ^ def save(self): ... |
| 294 | / \ |
| 295 | / \ |
| 296 | / \ |
| 297 | / \ |
| 298 | class B class C: |
| 299 | ^ ^ def save(self): ... |
| 300 | \ / |
| 301 | \ / |
| 302 | \ / |
| 303 | \ / |
| 304 | class D |
| 305 | \end{verbatim} |
| 306 | |
| 307 | The lookup rule for classic classes is simple but not very smart; the |
| 308 | base classes are searched depth-first, going from left to right. A |
| 309 | reference to \method{D.save} will search the classes \class{D}, |
| 310 | \class{B}, and then \class{A}, where \method{save()} would be found |
| 311 | and returned. \method{C.save()} would never be found at all. This is |
| 312 | bad, because if \class{C}'s \method{save()} method is saving some |
| 313 | internal state specific to \class{C}, not calling it will result in |
| 314 | that state never getting saved. |
| 315 | |
| 316 | New-style classes follow a different algorithm that's a bit more |
| 317 | complicated to explain, but does the right thing in this situation. |
| 318 | |
| 319 | \begin{enumerate} |
| 320 | |
| 321 | \item List all the base classes, following the classic lookup rule and |
| 322 | include a class multiple times if it's visited repeatedly. In the |
| 323 | above example, the list of visited classes is [\class{D}, \class{B}, |
Andrew M. Kuchling | 2836907 | 2001-10-29 15:47:33 +0000 | [diff] [blame] | 324 | \class{A}, \class{C}, \class{A}]. |
Andrew M. Kuchling | 4855b02 | 2001-10-23 20:26:16 +0000 | [diff] [blame] | 325 | |
| 326 | \item Scan the list for duplicated classes. If any are found, remove |
| 327 | all but one occurrence, leaving the \emph{last} one in the list. In |
| 328 | the above example, the list becomes [\class{D}, \class{B}, \class{C}, |
Andrew M. Kuchling | 2836907 | 2001-10-29 15:47:33 +0000 | [diff] [blame] | 329 | \class{A}] after dropping duplicates. |
Andrew M. Kuchling | 4855b02 | 2001-10-23 20:26:16 +0000 | [diff] [blame] | 330 | |
| 331 | \end{enumerate} |
| 332 | |
| 333 | Following this rule, referring to \method{D.save()} will return |
| 334 | \method{C.save()}, which is the behaviour we're after. This lookup |
Andrew M. Kuchling | b83769c | 2001-10-26 20:07:03 +0000 | [diff] [blame] | 335 | rule is the same as the one followed by Common Lisp. |
Andrew M. Kuchling | 4855b02 | 2001-10-23 20:26:16 +0000 | [diff] [blame] | 336 | |
Andrew M. Kuchling | 279e744 | 2001-10-22 02:03:40 +0000 | [diff] [blame] | 337 | |
| 338 | \subsection{Attribute Access} |
| 339 | |
Andrew M. Kuchling | b83769c | 2001-10-26 20:07:03 +0000 | [diff] [blame] | 340 | A fair number of sophisticated Python classes define hooks for |
| 341 | attribute access using \method{__getattr__}; most commonly this is |
| 342 | done for convenience, to make code more readable by automatically |
| 343 | mapping an attribute access such as \code{obj.parent} into a method |
| 344 | call such as \code{obj.get_parent()}. Python 2.2 adds some new ways |
| 345 | of controlling attribute access. |
Andrew M. Kuchling | 279e744 | 2001-10-22 02:03:40 +0000 | [diff] [blame] | 346 | |
Andrew M. Kuchling | b83769c | 2001-10-26 20:07:03 +0000 | [diff] [blame] | 347 | First, \method{__getattr__(\var{attr_name})} is still supported by |
| 348 | new-style classes, and nothing about it has changed. As before, it |
| 349 | will be called when an attempt is made to access \code{obj.foo} and no |
| 350 | attribute named \samp{foo} is found in the instance's dictionary. |
| 351 | |
| 352 | New-style classes also support a new method, |
| 353 | \method{__getattribute__(\var{attr_name})}. The difference between |
| 354 | the two methods is that \method{__getattribute__} is \emph{always} |
| 355 | called whenever any attribute is accessed, while the old |
| 356 | \method{__getattr__} is only called if \samp{foo} isn't found in the |
| 357 | instance's dictionary. |
| 358 | |
| 359 | However, Python 2.2's support for \dfn{properties} will often be a |
| 360 | simpler way to trap attribute references. Writing a |
| 361 | \method{__getattr__} method is complicated because to avoid recursion |
| 362 | you can't use regular attribute accesses inside them, and instead have |
| 363 | to mess around with the contents of \member{__dict__}. |
| 364 | \method{__getattr__} methods also end up being called by Python when |
| 365 | it checks for other methods such as \method{__repr__} or |
| 366 | \method{__coerce__}, and so have to be written with this in mind. |
| 367 | Finally, calling a function on every attribute access results in a |
| 368 | sizable performance loss. |
| 369 | |
| 370 | \class{property} is a new built-in type that packages up three |
| 371 | functions that get, set, or delete an attribute, and a docstring. For |
| 372 | example, if you want to define a \member{size} attribute that's |
| 373 | computed, but also settable, you could write: |
| 374 | |
| 375 | \begin{verbatim} |
| 376 | class C: |
| 377 | def get_size (self): |
| 378 | result = ... computation ... |
| 379 | return result |
| 380 | def set_size (self, size): |
| 381 | ... compute something based on the size |
| 382 | and set internal state appropriately ... |
| 383 | |
| 384 | # Define a property. The 'delete this attribute' |
| 385 | # method is defined as None, so the attribute |
| 386 | # can't be deleted. |
| 387 | size = property(get_size, set_size, |
| 388 | None, |
| 389 | "Storage size of this instance") |
| 390 | \end{verbatim} |
| 391 | |
| 392 | That is certainly clearer and easier to write than a pair of |
| 393 | \method{__getattr__}/\method{__setattr__} methods that check for the |
Andrew M. Kuchling | 7aa63c2 | 2001-10-30 14:35:03 +0000 | [diff] [blame^] | 394 | \member{size} attribute and handle it specially while retrieving all |
Andrew M. Kuchling | b83769c | 2001-10-26 20:07:03 +0000 | [diff] [blame] | 395 | other attributes from the instance's \member{__dict__}. Accesses to |
| 396 | \member{size} are also the only ones which have to perform the work of |
Andrew M. Kuchling | 7aa63c2 | 2001-10-30 14:35:03 +0000 | [diff] [blame^] | 397 | calling a function, so references to other attributes run at |
Andrew M. Kuchling | b83769c | 2001-10-26 20:07:03 +0000 | [diff] [blame] | 398 | their usual speed. |
| 399 | |
| 400 | Finally, it's possible to constrain the list of attributes that can be |
Andrew M. Kuchling | 7aa63c2 | 2001-10-30 14:35:03 +0000 | [diff] [blame^] | 401 | referenced on an object using the new \member{__slots__} class attribute. |
Andrew M. Kuchling | b83769c | 2001-10-26 20:07:03 +0000 | [diff] [blame] | 402 | Python objects are usually very dynamic; at any time it's possible to |
| 403 | define a new attribute on an instance by just doing |
| 404 | \code{obj.new_attr=1}. This is flexible and convenient, but this |
| 405 | flexibility can also lead to bugs, as when you meant to write |
Andrew M. Kuchling | 7aa63c2 | 2001-10-30 14:35:03 +0000 | [diff] [blame^] | 406 | \code{obj.template = 'a'} but made a typo and wrote |
Andrew M. Kuchling | b83769c | 2001-10-26 20:07:03 +0000 | [diff] [blame] | 407 | \code{obj.templtae} by accident. |
| 408 | |
Andrew M. Kuchling | 7aa63c2 | 2001-10-30 14:35:03 +0000 | [diff] [blame^] | 409 | A new-style class can define a class attribute named \member{__slots__} |
Andrew M. Kuchling | b83769c | 2001-10-26 20:07:03 +0000 | [diff] [blame] | 410 | to constrain the list of legal attribute names. An example will make |
| 411 | this clear: |
| 412 | |
| 413 | \begin{verbatim} |
| 414 | >>> class C(object): |
| 415 | ... __slots__ = ['template', 'name'] |
| 416 | ... |
| 417 | >>> obj = C() |
| 418 | >>> print obj.template |
| 419 | None |
Andrew M. Kuchling | 2836907 | 2001-10-29 15:47:33 +0000 | [diff] [blame] | 420 | >>> obj.template = 'Test' |
Andrew M. Kuchling | b83769c | 2001-10-26 20:07:03 +0000 | [diff] [blame] | 421 | >>> print obj.template |
| 422 | Test |
Andrew M. Kuchling | 2836907 | 2001-10-29 15:47:33 +0000 | [diff] [blame] | 423 | >>> obj.templtae = None |
Andrew M. Kuchling | b83769c | 2001-10-26 20:07:03 +0000 | [diff] [blame] | 424 | Traceback (most recent call last): |
| 425 | File "<stdin>", line 1, in ? |
| 426 | AttributeError: 'C' object has no attribute 'templtae' |
| 427 | \end{verbatim} |
| 428 | |
Andrew M. Kuchling | 7aa63c2 | 2001-10-30 14:35:03 +0000 | [diff] [blame^] | 429 | Note how you get an \exception{AttributeError} on the attempt to |
| 430 | assign to an attribute not listed in \member{__slots__}. |
Andrew M. Kuchling | 4855b02 | 2001-10-23 20:26:16 +0000 | [diff] [blame] | 431 | |
| 432 | |
Andrew M. Kuchling | 279e744 | 2001-10-22 02:03:40 +0000 | [diff] [blame] | 433 | \subsection{Related Links} |
Andrew M. Kuchling | cf31d5d | 2001-10-26 20:37:55 +0000 | [diff] [blame] | 434 | \label{sect-rellinks} |
Andrew M. Kuchling | 279e744 | 2001-10-22 02:03:40 +0000 | [diff] [blame] | 435 | |
| 436 | This section has just been a quick overview of the new features, |
| 437 | giving enough of an explanation to start you programming, but many |
| 438 | details have been simplified or ignored. Where should you go to get a |
| 439 | more complete picture? |
| 440 | |
Andrew M. Kuchling | b83769c | 2001-10-26 20:07:03 +0000 | [diff] [blame] | 441 | \url{http://www.python.org/2.2/descrintro.html} is a lengthy tutorial |
Andrew M. Kuchling | 279e744 | 2001-10-22 02:03:40 +0000 | [diff] [blame] | 442 | introduction to the descriptor features, written by Guido van Rossum. |
Andrew M. Kuchling | b83769c | 2001-10-26 20:07:03 +0000 | [diff] [blame] | 443 | If my description has whetted your appetite, go read this tutorial |
| 444 | next, because it goes into much more detail about the new features |
| 445 | while still remaining quite easy to read. |
Andrew M. Kuchling | 279e744 | 2001-10-22 02:03:40 +0000 | [diff] [blame] | 446 | |
| 447 | Next, there are two relevant PEPs, \pep{252} and \pep{253}. \pep{252} |
| 448 | is titled "Making Types Look More Like Classes", and covers the |
| 449 | descriptor API. \pep{253} is titled "Subtyping Built-in Types", and |
| 450 | describes the changes to type objects that make it possible to subtype |
Andrew M. Kuchling | b83769c | 2001-10-26 20:07:03 +0000 | [diff] [blame] | 451 | built-in objects. \pep{253} is the more complicated PEP of the two, |
| 452 | and at a few points the necessary explanations of types and meta-types |
| 453 | may cause your head to explode. Both PEPs were written and |
| 454 | implemented by Guido van Rossum, with substantial assistance from the |
| 455 | rest of the Zope Corp. team. |
Andrew M. Kuchling | 279e744 | 2001-10-22 02:03:40 +0000 | [diff] [blame] | 456 | |
Andrew M. Kuchling | b83769c | 2001-10-26 20:07:03 +0000 | [diff] [blame] | 457 | Finally, there's the ultimate authority: the source code. Most of the |
| 458 | machinery for the type handling is in \file{Objects/typeobject.c}, but |
| 459 | you should only resort to it after all other avenues have been |
Andrew M. Kuchling | 7aa63c2 | 2001-10-30 14:35:03 +0000 | [diff] [blame^] | 460 | exhausted, including posting a question to python-list or python-dev. |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 461 | |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 462 | |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 463 | %====================================================================== |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 464 | \section{PEP 234: Iterators} |
| 465 | |
Andrew M. Kuchling | 7aa63c2 | 2001-10-30 14:35:03 +0000 | [diff] [blame^] | 466 | Another significant addition to 2.2 is an iteration interface at both |
| 467 | the C and Python levels. Objects can define how they can be looped |
| 468 | over by callers. |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 469 | |
| 470 | In Python versions up to 2.1, the usual way to make \code{for item in |
| 471 | obj} work is to define a \method{__getitem__()} method that looks |
| 472 | something like this: |
| 473 | |
| 474 | \begin{verbatim} |
| 475 | def __getitem__(self, index): |
| 476 | return <next item> |
| 477 | \end{verbatim} |
| 478 | |
| 479 | \method{__getitem__()} is more properly used to define an indexing |
| 480 | operation on an object so that you can write \code{obj[5]} to retrieve |
Andrew M. Kuchling | 8c69c91 | 2001-08-07 14:28:58 +0000 | [diff] [blame] | 481 | the sixth element. It's a bit misleading when you're using this only |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 482 | to support \keyword{for} loops. Consider some file-like object that |
| 483 | wants to be looped over; the \var{index} parameter is essentially |
| 484 | meaningless, as the class probably assumes that a series of |
Andrew M. Kuchling | 7aa63c2 | 2001-10-30 14:35:03 +0000 | [diff] [blame^] | 485 | \method{__getitem__()} calls will be made with \var{index} |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 486 | incrementing by one each time. In other words, the presence of the |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 487 | \method{__getitem__()} method doesn't mean that using \code{file[5]} |
| 488 | to randomly access the sixth element will work, though it really should. |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 489 | |
| 490 | In Python 2.2, iteration can be implemented separately, and |
| 491 | \method{__getitem__()} methods can be limited to classes that really |
Andrew M. Kuchling | 7aa63c2 | 2001-10-30 14:35:03 +0000 | [diff] [blame^] | 492 | do support random access. The basic idea of iterators is |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 493 | simple. A new built-in function, \function{iter(obj)} or |
| 494 | \code{iter(\var{C}, \var{sentinel})}, is used to get an iterator. |
| 495 | \function{iter(obj)} returns an iterator for the object \var{obj}, |
| 496 | while \code{iter(\var{C}, \var{sentinel})} returns an iterator that |
| 497 | will invoke the callable object \var{C} until it returns |
| 498 | \var{sentinel} to signal that the iterator is done. |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 499 | |
| 500 | Python classes can define an \method{__iter__()} method, which should |
| 501 | create and return a new iterator for the object; if the object is its |
| 502 | own iterator, this method can just return \code{self}. In particular, |
| 503 | iterators will usually be their own iterators. Extension types |
| 504 | implemented in C can implement a \code{tp_iter} function in order to |
Andrew M. Kuchling | 4cf52a9 | 2001-07-17 12:48:48 +0000 | [diff] [blame] | 505 | return an iterator, and extension types that want to behave as |
| 506 | iterators can define a \code{tp_iternext} function. |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 507 | |
Andrew M. Kuchling | 7aa63c2 | 2001-10-30 14:35:03 +0000 | [diff] [blame^] | 508 | So, after all this, what do iterators actually do? They have one |
| 509 | required method, \method{next()}, which takes no arguments and returns |
| 510 | the next value. When there are no more values to be returned, calling |
| 511 | \method{next()} should raise the \exception{StopIteration} exception. |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 512 | |
| 513 | \begin{verbatim} |
| 514 | >>> L = [1,2,3] |
| 515 | >>> i = iter(L) |
| 516 | >>> print i |
| 517 | <iterator object at 0x8116870> |
| 518 | >>> i.next() |
| 519 | 1 |
| 520 | >>> i.next() |
| 521 | 2 |
| 522 | >>> i.next() |
| 523 | 3 |
| 524 | >>> i.next() |
| 525 | Traceback (most recent call last): |
| 526 | File "<stdin>", line 1, in ? |
| 527 | StopIteration |
| 528 | >>> |
| 529 | \end{verbatim} |
| 530 | |
| 531 | In 2.2, Python's \keyword{for} statement no longer expects a sequence; |
Andrew M. Kuchling | 7aa63c2 | 2001-10-30 14:35:03 +0000 | [diff] [blame^] | 532 | it expects something for which \function{iter()} will return an iterator. |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 533 | For backward compatibility and convenience, an iterator is |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 534 | automatically constructed for sequences that don't implement |
| 535 | \method{__iter__()} or a \code{tp_iter} slot, so \code{for i in |
| 536 | [1,2,3]} will still work. Wherever the Python interpreter loops over |
| 537 | a sequence, it's been changed to use the iterator protocol. This |
| 538 | means you can do things like this: |
| 539 | |
| 540 | \begin{verbatim} |
Andrew M. Kuchling | 7aa63c2 | 2001-10-30 14:35:03 +0000 | [diff] [blame^] | 541 | >>> L = [1,2,3] |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 542 | >>> i = iter(L) |
| 543 | >>> a,b,c = i |
| 544 | >>> a,b,c |
| 545 | (1, 2, 3) |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 546 | \end{verbatim} |
| 547 | |
Andrew M. Kuchling | 9e9c135 | 2001-08-11 03:06:50 +0000 | [diff] [blame] | 548 | Iterator support has been added to some of Python's basic types. |
Fred Drake | 0d00254 | 2001-07-17 13:55:33 +0000 | [diff] [blame] | 549 | Calling \function{iter()} on a dictionary will return an iterator |
Andrew M. Kuchling | 6ea9f0b | 2001-07-17 14:50:31 +0000 | [diff] [blame] | 550 | which loops over its keys: |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 551 | |
| 552 | \begin{verbatim} |
| 553 | >>> m = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6, |
| 554 | ... 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12} |
| 555 | >>> for key in m: print key, m[key] |
| 556 | ... |
| 557 | Mar 3 |
| 558 | Feb 2 |
| 559 | Aug 8 |
| 560 | Sep 9 |
| 561 | May 5 |
| 562 | Jun 6 |
| 563 | Jul 7 |
| 564 | Jan 1 |
| 565 | Apr 4 |
| 566 | Nov 11 |
| 567 | Dec 12 |
| 568 | Oct 10 |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 569 | \end{verbatim} |
| 570 | |
| 571 | That's just the default behaviour. If you want to iterate over keys, |
| 572 | values, or key/value pairs, you can explicitly call the |
| 573 | \method{iterkeys()}, \method{itervalues()}, or \method{iteritems()} |
Andrew M. Kuchling | 9e9c135 | 2001-08-11 03:06:50 +0000 | [diff] [blame] | 574 | methods to get an appropriate iterator. In a minor related change, |
| 575 | the \keyword{in} operator now works on dictionaries, so |
| 576 | \code{\var{key} in dict} is now equivalent to |
| 577 | \code{dict.has_key(\var{key})}. |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 578 | |
Andrew M. Kuchling | 9e9c135 | 2001-08-11 03:06:50 +0000 | [diff] [blame] | 579 | Files also provide an iterator, which calls the \method{readline()} |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 580 | method until there are no more lines in the file. This means you can |
| 581 | now read each line of a file using code like this: |
| 582 | |
| 583 | \begin{verbatim} |
| 584 | for line in file: |
| 585 | # do something for each line |
Andrew M. Kuchling | 7aa63c2 | 2001-10-30 14:35:03 +0000 | [diff] [blame^] | 586 | ... |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 587 | \end{verbatim} |
| 588 | |
| 589 | Note that you can only go forward in an iterator; there's no way to |
| 590 | get the previous element, reset the iterator, or make a copy of it. |
Fred Drake | 0d00254 | 2001-07-17 13:55:33 +0000 | [diff] [blame] | 591 | An iterator object could provide such additional capabilities, but the |
| 592 | iterator protocol only requires a \method{next()} method. |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 593 | |
| 594 | \begin{seealso} |
| 595 | |
| 596 | \seepep{234}{Iterators}{Written by Ka-Ping Yee and GvR; implemented |
| 597 | by the Python Labs crew, mostly by GvR and Tim Peters.} |
| 598 | |
| 599 | \end{seealso} |
| 600 | |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 601 | |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 602 | %====================================================================== |
| 603 | \section{PEP 255: Simple Generators} |
| 604 | |
| 605 | Generators are another new feature, one that interacts with the |
| 606 | introduction of iterators. |
| 607 | |
| 608 | You're doubtless familiar with how function calls work in Python or |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 609 | C. When you call a function, it gets a private namespace where its local |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 610 | variables are created. When the function reaches a \keyword{return} |
| 611 | statement, the local variables are destroyed and the resulting value |
| 612 | is returned to the caller. A later call to the same function will get |
| 613 | a fresh new set of local variables. But, what if the local variables |
Andrew M. Kuchling | 7aa63c2 | 2001-10-30 14:35:03 +0000 | [diff] [blame^] | 614 | weren't thrown away on exiting a function? What if you could later |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 615 | resume the function where it left off? This is what generators |
| 616 | provide; they can be thought of as resumable functions. |
| 617 | |
| 618 | Here's the simplest example of a generator function: |
| 619 | |
| 620 | \begin{verbatim} |
| 621 | def generate_ints(N): |
| 622 | for i in range(N): |
| 623 | yield i |
| 624 | \end{verbatim} |
| 625 | |
| 626 | A new keyword, \keyword{yield}, was introduced for generators. Any |
| 627 | function containing a \keyword{yield} statement is a generator |
| 628 | function; this is detected by Python's bytecode compiler which |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 629 | compiles the function specially as a result. Because a new keyword was |
Andrew M. Kuchling | 4cf52a9 | 2001-07-17 12:48:48 +0000 | [diff] [blame] | 630 | introduced, generators must be explicitly enabled in a module by |
| 631 | including a \code{from __future__ import generators} statement near |
| 632 | the top of the module's source code. In Python 2.3 this statement |
| 633 | will become unnecessary. |
| 634 | |
| 635 | When you call a generator function, it doesn't return a single value; |
| 636 | instead it returns a generator object that supports the iterator |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 637 | protocol. On executing the \keyword{yield} statement, the generator |
Andrew M. Kuchling | 4cf52a9 | 2001-07-17 12:48:48 +0000 | [diff] [blame] | 638 | outputs the value of \code{i}, similar to a \keyword{return} |
| 639 | statement. The big difference between \keyword{yield} and a |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 640 | \keyword{return} statement is that on reaching a \keyword{yield} the |
Andrew M. Kuchling | 4cf52a9 | 2001-07-17 12:48:48 +0000 | [diff] [blame] | 641 | generator's state of execution is suspended and local variables are |
| 642 | preserved. On the next call to the generator's \code{.next()} method, |
| 643 | the function will resume executing immediately after the |
| 644 | \keyword{yield} statement. (For complicated reasons, the |
| 645 | \keyword{yield} statement isn't allowed inside the \keyword{try} block |
Andrew M. Kuchling | cf31d5d | 2001-10-26 20:37:55 +0000 | [diff] [blame] | 646 | of a \code{try...finally} statement; read \pep{255} for a full |
Andrew M. Kuchling | 4cf52a9 | 2001-07-17 12:48:48 +0000 | [diff] [blame] | 647 | explanation of the interaction between \keyword{yield} and |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 648 | exceptions.) |
| 649 | |
| 650 | Here's a sample usage of the \function{generate_ints} generator: |
| 651 | |
| 652 | \begin{verbatim} |
| 653 | >>> gen = generate_ints(3) |
| 654 | >>> gen |
| 655 | <generator object at 0x8117f90> |
| 656 | >>> gen.next() |
| 657 | 0 |
| 658 | >>> gen.next() |
| 659 | 1 |
| 660 | >>> gen.next() |
| 661 | 2 |
| 662 | >>> gen.next() |
| 663 | Traceback (most recent call last): |
| 664 | File "<stdin>", line 1, in ? |
| 665 | File "<stdin>", line 2, in generate_ints |
| 666 | StopIteration |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 667 | \end{verbatim} |
| 668 | |
| 669 | You could equally write \code{for i in generate_ints(5)}, or |
| 670 | \code{a,b,c = generate_ints(3)}. |
| 671 | |
| 672 | Inside a generator function, the \keyword{return} statement can only |
Andrew M. Kuchling | 4cf52a9 | 2001-07-17 12:48:48 +0000 | [diff] [blame] | 673 | be used without a value, and signals the end of the procession of |
| 674 | values; afterwards the generator cannot return any further values. |
| 675 | \keyword{return} with a value, such as \code{return 5}, is a syntax |
| 676 | error inside a generator function. The end of the generator's results |
| 677 | can also be indicated by raising \exception{StopIteration} manually, |
| 678 | or by just letting the flow of execution fall off the bottom of the |
| 679 | function. |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 680 | |
| 681 | You could achieve the effect of generators manually by writing your |
Andrew M. Kuchling | 4cf52a9 | 2001-07-17 12:48:48 +0000 | [diff] [blame] | 682 | own class and storing all the local variables of the generator as |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 683 | instance variables. For example, returning a list of integers could |
| 684 | be done by setting \code{self.count} to 0, and having the |
| 685 | \method{next()} method increment \code{self.count} and return it. |
Andrew M. Kuchling | c32cc7c | 2001-07-17 18:25:01 +0000 | [diff] [blame] | 686 | However, for a moderately complicated generator, writing a |
| 687 | corresponding class would be much messier. |
| 688 | \file{Lib/test/test_generators.py} contains a number of more |
| 689 | interesting examples. The simplest one implements an in-order |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 690 | traversal of a tree using generators recursively. |
| 691 | |
| 692 | \begin{verbatim} |
| 693 | # A recursive generator that generates Tree leaves in in-order. |
| 694 | def inorder(t): |
| 695 | if t: |
| 696 | for x in inorder(t.left): |
| 697 | yield x |
| 698 | yield t.label |
| 699 | for x in inorder(t.right): |
| 700 | yield x |
| 701 | \end{verbatim} |
| 702 | |
| 703 | Two other examples in \file{Lib/test/test_generators.py} produce |
| 704 | solutions for the N-Queens problem (placing $N$ queens on an $NxN$ |
| 705 | chess board so that no queen threatens another) and the Knight's Tour |
| 706 | (a route that takes a knight to every square of an $NxN$ chessboard |
| 707 | without visiting any square twice). |
| 708 | |
| 709 | The idea of generators comes from other programming languages, |
| 710 | especially Icon (\url{http://www.cs.arizona.edu/icon/}), where the |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 711 | idea of generators is central. In Icon, every |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 712 | expression and function call behaves like a generator. One example |
| 713 | from ``An Overview of the Icon Programming Language'' at |
| 714 | \url{http://www.cs.arizona.edu/icon/docs/ipd266.htm} gives an idea of |
| 715 | what this looks like: |
| 716 | |
| 717 | \begin{verbatim} |
| 718 | sentence := "Store it in the neighboring harbor" |
| 719 | if (i := find("or", sentence)) > 5 then write(i) |
| 720 | \end{verbatim} |
| 721 | |
Andrew M. Kuchling | 7aa63c2 | 2001-10-30 14:35:03 +0000 | [diff] [blame^] | 722 | In Icon the \function{find()} function returns the indexes at which the |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 723 | substring ``or'' is found: 3, 23, 33. In the \keyword{if} statement, |
| 724 | \code{i} is first assigned a value of 3, but 3 is less than 5, so the |
| 725 | comparison fails, and Icon retries it with the second value of 23. 23 |
| 726 | is greater than 5, so the comparison now succeeds, and the code prints |
| 727 | the value 23 to the screen. |
| 728 | |
| 729 | Python doesn't go nearly as far as Icon in adopting generators as a |
| 730 | central concept. Generators are considered a new part of the core |
| 731 | Python language, but learning or using them isn't compulsory; if they |
| 732 | don't solve any problems that you have, feel free to ignore them. |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 733 | One novel feature of Python's interface as compared to |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 734 | Icon's is that a generator's state is represented as a concrete object |
Andrew M. Kuchling | 7aa63c2 | 2001-10-30 14:35:03 +0000 | [diff] [blame^] | 735 | (the iterator) that can be passed around to other functions or stored |
| 736 | in a data structure. |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 737 | |
| 738 | \begin{seealso} |
| 739 | |
Andrew M. Kuchling | 4cf52a9 | 2001-07-17 12:48:48 +0000 | [diff] [blame] | 740 | \seepep{255}{Simple Generators}{Written by Neil Schemenauer, Tim |
| 741 | Peters, Magnus Lie Hetland. Implemented mostly by Neil Schemenauer |
| 742 | and Tim Peters, with other fixes from the Python Labs crew.} |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 743 | |
| 744 | \end{seealso} |
| 745 | |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 746 | |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 747 | %====================================================================== |
Andrew M. Kuchling | 2f0047a | 2001-09-05 14:53:31 +0000 | [diff] [blame] | 748 | \section{PEP 237: Unifying Long Integers and Integers} |
| 749 | |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 750 | In recent versions, the distinction between regular integers, which |
| 751 | are 32-bit values on most machines, and long integers, which can be of |
| 752 | arbitrary size, was becoming an annoyance. For example, on platforms |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 753 | that support files larger than \code{2**32} bytes, the |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 754 | \method{tell()} method of file objects has to return a long integer. |
| 755 | However, there were various bits of Python that expected plain |
| 756 | integers and would raise an error if a long integer was provided |
Andrew M. Kuchling | d6e40e2 | 2001-09-10 16:18:50 +0000 | [diff] [blame] | 757 | instead. For example, in Python 1.5, only regular integers |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 758 | could be used as a slice index, and \code{'abc'[1L:]} would raise a |
| 759 | \exception{TypeError} exception with the message 'slice index must be |
| 760 | int'. |
Andrew M. Kuchling | 2f0047a | 2001-09-05 14:53:31 +0000 | [diff] [blame] | 761 | |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 762 | Python 2.2 will shift values from short to long integers as required. |
| 763 | The 'L' suffix is no longer needed to indicate a long integer literal, |
| 764 | as now the compiler will choose the appropriate type. (Using the 'L' |
| 765 | suffix will be discouraged in future 2.x versions of Python, |
| 766 | triggering a warning in Python 2.4, and probably dropped in Python |
| 767 | 3.0.) Many operations that used to raise an \exception{OverflowError} |
| 768 | will now return a long integer as their result. For example: |
| 769 | |
| 770 | \begin{verbatim} |
| 771 | >>> 1234567890123 |
Andrew M. Kuchling | d6e40e2 | 2001-09-10 16:18:50 +0000 | [diff] [blame] | 772 | 1234567890123L |
| 773 | >>> 2 ** 64 |
| 774 | 18446744073709551616L |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 775 | \end{verbatim} |
| 776 | |
| 777 | In most cases, integers and long integers will now be treated |
| 778 | identically. You can still distinguish them with the |
Andrew M. Kuchling | 7aa63c2 | 2001-10-30 14:35:03 +0000 | [diff] [blame^] | 779 | \function{type()} built-in function, but that's rarely needed. |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 780 | |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 781 | \begin{seealso} |
| 782 | |
| 783 | \seepep{237}{Unifying Long Integers and Integers}{Written by |
Andrew M. Kuchling | 7aa63c2 | 2001-10-30 14:35:03 +0000 | [diff] [blame^] | 784 | Moshe Zadka and Guido van Rossum. Implemented mostly by Guido van |
| 785 | Rossum.} |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 786 | |
| 787 | \end{seealso} |
Andrew M. Kuchling | 2f0047a | 2001-09-05 14:53:31 +0000 | [diff] [blame] | 788 | |
Andrew M. Kuchling | d4707e3 | 2001-09-28 20:46:46 +0000 | [diff] [blame] | 789 | |
Andrew M. Kuchling | 2f0047a | 2001-09-05 14:53:31 +0000 | [diff] [blame] | 790 | %====================================================================== |
Andrew M. Kuchling | 9e9c135 | 2001-08-11 03:06:50 +0000 | [diff] [blame] | 791 | \section{PEP 238: Changing the Division Operator} |
| 792 | |
Andrew M. Kuchling | 7aa63c2 | 2001-10-30 14:35:03 +0000 | [diff] [blame^] | 793 | The most controversial change in Python 2.2 heralds the start of an effort |
Andrew M. Kuchling | 9e9c135 | 2001-08-11 03:06:50 +0000 | [diff] [blame] | 794 | to fix an old design flaw that's been in Python from the beginning. |
| 795 | Currently Python's division operator, \code{/}, behaves like C's |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 796 | division operator when presented with two integer arguments: it |
Andrew M. Kuchling | 9e9c135 | 2001-08-11 03:06:50 +0000 | [diff] [blame] | 797 | returns an integer result that's truncated down when there would be |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 798 | a fractional part. For example, \code{3/2} is 1, not 1.5, and |
Andrew M. Kuchling | 9e9c135 | 2001-08-11 03:06:50 +0000 | [diff] [blame] | 799 | \code{(-1)/2} is -1, not -0.5. This means that the results of divison |
| 800 | can vary unexpectedly depending on the type of the two operands and |
| 801 | because Python is dynamically typed, it can be difficult to determine |
| 802 | the possible types of the operands. |
| 803 | |
| 804 | (The controversy is over whether this is \emph{really} a design flaw, |
| 805 | and whether it's worth breaking existing code to fix this. It's |
Andrew M. Kuchling | 7aa63c2 | 2001-10-30 14:35:03 +0000 | [diff] [blame^] | 806 | caused endless discussions on python-dev, and in July 2001 erupted into an |
Andrew M. Kuchling | 9e9c135 | 2001-08-11 03:06:50 +0000 | [diff] [blame] | 807 | storm of acidly sarcastic postings on \newsgroup{comp.lang.python}. I |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 808 | won't argue for either side here and will stick to describing what's |
Andrew M. Kuchling | cf31d5d | 2001-10-26 20:37:55 +0000 | [diff] [blame] | 809 | implemented in 2.2. Read \pep{238} for a summary of arguments and |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 810 | counter-arguments.) |
Andrew M. Kuchling | 9e9c135 | 2001-08-11 03:06:50 +0000 | [diff] [blame] | 811 | |
| 812 | Because this change might break code, it's being introduced very |
| 813 | gradually. Python 2.2 begins the transition, but the switch won't be |
| 814 | complete until Python 3.0. |
| 815 | |
Andrew M. Kuchling | cf31d5d | 2001-10-26 20:37:55 +0000 | [diff] [blame] | 816 | First, I'll borrow some terminology from \pep{238}. ``True division'' is the |
Andrew M. Kuchling | 9e9c135 | 2001-08-11 03:06:50 +0000 | [diff] [blame] | 817 | division that most non-programmers are familiar with: 3/2 is 1.5, 1/4 |
| 818 | is 0.25, and so forth. ``Floor division'' is what Python's \code{/} |
| 819 | operator currently does when given integer operands; the result is the |
| 820 | floor of the value returned by true division. ``Classic division'' is |
| 821 | the current mixed behaviour of \code{/}; it returns the result of |
| 822 | floor division when the operands are integers, and returns the result |
| 823 | of true division when one of the operands is a floating-point number. |
| 824 | |
| 825 | Here are the changes 2.2 introduces: |
| 826 | |
| 827 | \begin{itemize} |
| 828 | |
| 829 | \item A new operator, \code{//}, is the floor division operator. |
| 830 | (Yes, we know it looks like \Cpp's comment symbol.) \code{//} |
Andrew M. Kuchling | 7aa63c2 | 2001-10-30 14:35:03 +0000 | [diff] [blame^] | 831 | \emph{always} performs floor division no matter what the types of |
Andrew M. Kuchling | 9e9c135 | 2001-08-11 03:06:50 +0000 | [diff] [blame] | 832 | its operands are, so \code{1 // 2} is 0 and \code{1.0 // 2.0} is also |
| 833 | 0.0. |
| 834 | |
| 835 | \code{//} is always available in Python 2.2; you don't need to enable |
| 836 | it using a \code{__future__} statement. |
| 837 | |
Andrew M. Kuchling | 4f9e220 | 2001-10-29 18:09:42 +0000 | [diff] [blame] | 838 | \item By including a \code{from __future__ import division} in a |
Andrew M. Kuchling | 9e9c135 | 2001-08-11 03:06:50 +0000 | [diff] [blame] | 839 | module, the \code{/} operator will be changed to return the result of |
| 840 | true division, so \code{1/2} is 0.5. Without the \code{__future__} |
| 841 | statement, \code{/} still means classic division. The default meaning |
| 842 | of \code{/} will not change until Python 3.0. |
| 843 | |
| 844 | \item Classes can define methods called \method{__truediv__} and |
| 845 | \method{__floordiv__} to overload the two division operators. At the |
| 846 | C level, there are also slots in the \code{PyNumberMethods} structure |
| 847 | so extension types can define the two operators. |
| 848 | |
Andrew M. Kuchling | cf31d5d | 2001-10-26 20:37:55 +0000 | [diff] [blame] | 849 | \item Python 2.2 supports some command-line arguments for testing |
| 850 | whether code will works with the changed division semantics. Running |
| 851 | python with \programopt{-Q warn} will cause a warning to be issued |
| 852 | whenever division is applied to two integers. You can use this to |
| 853 | find code that's affected by the change and fix it. By default, |
| 854 | Python 2.2 will simply perform classic division without a warning; the |
| 855 | warning will be turned on by default in Python 2.3. |
Andrew M. Kuchling | 9e9c135 | 2001-08-11 03:06:50 +0000 | [diff] [blame] | 856 | |
| 857 | \end{itemize} |
| 858 | |
| 859 | \begin{seealso} |
| 860 | |
| 861 | \seepep{238}{Changing the Division Operator}{Written by Moshe Zadka and |
| 862 | Guido van Rossum. Implemented by Guido van Rossum..} |
| 863 | |
| 864 | \end{seealso} |
| 865 | |
| 866 | |
| 867 | %====================================================================== |
Andrew M. Kuchling | a43e703 | 2001-06-27 20:32:12 +0000 | [diff] [blame] | 868 | \section{Unicode Changes} |
| 869 | |
Andrew M. Kuchling | 2cd712b | 2001-07-16 13:39:08 +0000 | [diff] [blame] | 870 | Python's Unicode support has been enhanced a bit in 2.2. Unicode |
Andrew M. Kuchling | a6d2a04 | 2001-07-20 18:34:34 +0000 | [diff] [blame] | 871 | strings are usually stored as UCS-2, as 16-bit unsigned integers. |
Andrew M. Kuchling | f5fec3c | 2001-07-19 01:48:08 +0000 | [diff] [blame] | 872 | Python 2.2 can also be compiled to use UCS-4, 32-bit unsigned |
| 873 | integers, as its internal encoding by supplying |
| 874 | \longprogramopt{enable-unicode=ucs4} to the configure script. When |
Andrew M. Kuchling | ab01087 | 2001-07-19 14:59:53 +0000 | [diff] [blame] | 875 | built to use UCS-4 (a ``wide Python''), the interpreter can natively |
Andrew M. Kuchling | a6d2a04 | 2001-07-20 18:34:34 +0000 | [diff] [blame] | 876 | handle Unicode characters from U+000000 to U+110000, so the range of |
| 877 | legal values for the \function{unichr()} function is expanded |
| 878 | accordingly. Using an interpreter compiled to use UCS-2 (a ``narrow |
| 879 | Python''), values greater than 65535 will still cause |
| 880 | \function{unichr()} to raise a \exception{ValueError} exception. |
Andrew M. Kuchling | ab01087 | 2001-07-19 14:59:53 +0000 | [diff] [blame] | 881 | |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 882 | % XXX is this still unimplemented? |
Andrew M. Kuchling | cf31d5d | 2001-10-26 20:37:55 +0000 | [diff] [blame] | 883 | All this is the province of the still-unimplemented \pep{261}, ``Support |
Andrew M. Kuchling | ab01087 | 2001-07-19 14:59:53 +0000 | [diff] [blame] | 884 | for `wide' Unicode characters''; consult it for further details, and |
Andrew M. Kuchling | a6d2a04 | 2001-07-20 18:34:34 +0000 | [diff] [blame] | 885 | please offer comments on the PEP and on your experiences with the |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 886 | 2.2 beta releases. |
Andrew M. Kuchling | 279e744 | 2001-10-22 02:03:40 +0000 | [diff] [blame] | 887 | % XXX update previous line once 2.2 reaches beta or final. |
Andrew M. Kuchling | ab01087 | 2001-07-19 14:59:53 +0000 | [diff] [blame] | 888 | |
| 889 | Another change is much simpler to explain. Since their introduction, |
| 890 | Unicode strings have supported an \method{encode()} method to convert |
| 891 | the string to a selected encoding such as UTF-8 or Latin-1. A |
| 892 | symmetric \method{decode(\optional{\var{encoding}})} method has been |
| 893 | added to 8-bit strings (though not to Unicode strings) in 2.2. |
| 894 | \method{decode()} assumes that the string is in the specified encoding |
| 895 | and decodes it, returning whatever is returned by the codec. |
| 896 | |
| 897 | Using this new feature, codecs have been added for tasks not directly |
| 898 | related to Unicode. For example, codecs have been added for |
| 899 | uu-encoding, MIME's base64 encoding, and compression with the |
| 900 | \module{zlib} module: |
Andrew M. Kuchling | 2cd712b | 2001-07-16 13:39:08 +0000 | [diff] [blame] | 901 | |
| 902 | \begin{verbatim} |
| 903 | >>> s = """Here is a lengthy piece of redundant, overly verbose, |
| 904 | ... and repetitive text. |
| 905 | ... """ |
| 906 | >>> data = s.encode('zlib') |
| 907 | >>> data |
| 908 | 'x\x9c\r\xc9\xc1\r\x80 \x10\x04\xc0?Ul...' |
| 909 | >>> data.decode('zlib') |
| 910 | 'Here is a lengthy piece of redundant, overly verbose,\nand repetitive text.\n' |
| 911 | >>> print s.encode('uu') |
| 912 | begin 666 <data> |
| 913 | M2&5R92!I<R!A(&QE;F=T:'D@<&EE8V4@;V8@<F5D=6YD86YT+"!O=F5R;'D@ |
| 914 | >=F5R8F]S92P*86YD(')E<&5T:71I=F4@=&5X="X* |
| 915 | |
| 916 | end |
| 917 | >>> "sheesh".encode('rot-13') |
| 918 | 'furrfu' |
| 919 | \end{verbatim} |
Andrew M. Kuchling | a43e703 | 2001-06-27 20:32:12 +0000 | [diff] [blame] | 920 | |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 921 | To convert a class instance to Unicode, a \method{__unicode__} method |
Andrew M. Kuchling | cf31d5d | 2001-10-26 20:37:55 +0000 | [diff] [blame] | 922 | can be defined by a class, analogous to \method{__str__}. |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 923 | |
Andrew M. Kuchling | cf31d5d | 2001-10-26 20:37:55 +0000 | [diff] [blame] | 924 | \method{encode()}, \method{decode()}, and \method{__unicode__} were |
| 925 | implemented by Marc-Andr\'e Lemburg. The changes to support using |
| 926 | UCS-4 internally were implemented by Fredrik Lundh and Martin von |
| 927 | L\"owis. |
Andrew M. Kuchling | a43e703 | 2001-06-27 20:32:12 +0000 | [diff] [blame] | 928 | |
Andrew M. Kuchling | f5fec3c | 2001-07-19 01:48:08 +0000 | [diff] [blame] | 929 | \begin{seealso} |
| 930 | |
| 931 | \seepep{261}{Support for `wide' Unicode characters}{PEP written by |
| 932 | Paul Prescod. Not yet accepted or fully implemented.} |
| 933 | |
| 934 | \end{seealso} |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 935 | |
Andrew M. Kuchling | cf31d5d | 2001-10-26 20:37:55 +0000 | [diff] [blame] | 936 | |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 937 | %====================================================================== |
| 938 | \section{PEP 227: Nested Scopes} |
| 939 | |
| 940 | In Python 2.1, statically nested scopes were added as an optional |
| 941 | feature, to be enabled by a \code{from __future__ import |
| 942 | nested_scopes} directive. In 2.2 nested scopes no longer need to be |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 943 | specially enabled, and are now always present. The rest of this section |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 944 | is a copy of the description of nested scopes from my ``What's New in |
| 945 | Python 2.1'' document; if you read it when 2.1 came out, you can skip |
| 946 | the rest of this section. |
| 947 | |
| 948 | The largest change introduced in Python 2.1, and made complete in 2.2, |
| 949 | is to Python's scoping rules. In Python 2.0, at any given time there |
| 950 | are at most three namespaces used to look up variable names: local, |
| 951 | module-level, and the built-in namespace. This often surprised people |
| 952 | because it didn't match their intuitive expectations. For example, a |
| 953 | nested recursive function definition doesn't work: |
| 954 | |
| 955 | \begin{verbatim} |
| 956 | def f(): |
| 957 | ... |
| 958 | def g(value): |
| 959 | ... |
| 960 | return g(value-1) + 1 |
| 961 | ... |
| 962 | \end{verbatim} |
| 963 | |
| 964 | The function \function{g()} will always raise a \exception{NameError} |
| 965 | exception, because the binding of the name \samp{g} isn't in either |
| 966 | its local namespace or in the module-level namespace. This isn't much |
| 967 | of a problem in practice (how often do you recursively define interior |
| 968 | functions like this?), but this also made using the \keyword{lambda} |
| 969 | statement clumsier, and this was a problem in practice. In code which |
| 970 | uses \keyword{lambda} you can often find local variables being copied |
| 971 | by passing them as the default values of arguments. |
| 972 | |
| 973 | \begin{verbatim} |
| 974 | def find(self, name): |
| 975 | "Return list of any entries equal to 'name'" |
| 976 | L = filter(lambda x, name=name: x == name, |
| 977 | self.list_attribute) |
| 978 | return L |
| 979 | \end{verbatim} |
| 980 | |
| 981 | The readability of Python code written in a strongly functional style |
| 982 | suffers greatly as a result. |
| 983 | |
| 984 | The most significant change to Python 2.2 is that static scoping has |
| 985 | been added to the language to fix this problem. As a first effect, |
| 986 | the \code{name=name} default argument is now unnecessary in the above |
| 987 | example. Put simply, when a given variable name is not assigned a |
| 988 | value within a function (by an assignment, or the \keyword{def}, |
| 989 | \keyword{class}, or \keyword{import} statements), references to the |
| 990 | variable will be looked up in the local namespace of the enclosing |
| 991 | scope. A more detailed explanation of the rules, and a dissection of |
| 992 | the implementation, can be found in the PEP. |
| 993 | |
| 994 | This change may cause some compatibility problems for code where the |
| 995 | same variable name is used both at the module level and as a local |
| 996 | variable within a function that contains further function definitions. |
| 997 | This seems rather unlikely though, since such code would have been |
| 998 | pretty confusing to read in the first place. |
| 999 | |
| 1000 | One side effect of the change is that the \code{from \var{module} |
| 1001 | import *} and \keyword{exec} statements have been made illegal inside |
| 1002 | a function scope under certain conditions. The Python reference |
| 1003 | manual has said all along that \code{from \var{module} import *} is |
| 1004 | only legal at the top level of a module, but the CPython interpreter |
| 1005 | has never enforced this before. As part of the implementation of |
| 1006 | nested scopes, the compiler which turns Python source into bytecodes |
| 1007 | has to generate different code to access variables in a containing |
| 1008 | scope. \code{from \var{module} import *} and \keyword{exec} make it |
| 1009 | impossible for the compiler to figure this out, because they add names |
| 1010 | to the local namespace that are unknowable at compile time. |
| 1011 | Therefore, if a function contains function definitions or |
| 1012 | \keyword{lambda} expressions with free variables, the compiler will |
| 1013 | flag this by raising a \exception{SyntaxError} exception. |
| 1014 | |
| 1015 | To make the preceding explanation a bit clearer, here's an example: |
| 1016 | |
| 1017 | \begin{verbatim} |
| 1018 | x = 1 |
| 1019 | def f(): |
| 1020 | # The next line is a syntax error |
| 1021 | exec 'x=2' |
| 1022 | def g(): |
| 1023 | return x |
| 1024 | \end{verbatim} |
| 1025 | |
| 1026 | Line 4 containing the \keyword{exec} statement is a syntax error, |
| 1027 | since \keyword{exec} would define a new local variable named \samp{x} |
| 1028 | whose value should be accessed by \function{g()}. |
| 1029 | |
| 1030 | This shouldn't be much of a limitation, since \keyword{exec} is rarely |
| 1031 | used in most Python code (and when it is used, it's often a sign of a |
| 1032 | poor design anyway). |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 1033 | |
| 1034 | \begin{seealso} |
| 1035 | |
| 1036 | \seepep{227}{Statically Nested Scopes}{Written and implemented by |
| 1037 | Jeremy Hylton.} |
| 1038 | |
| 1039 | \end{seealso} |
| 1040 | |
Andrew M. Kuchling | a43e703 | 2001-06-27 20:32:12 +0000 | [diff] [blame] | 1041 | |
| 1042 | %====================================================================== |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 1043 | \section{New and Improved Modules} |
| 1044 | |
| 1045 | \begin{itemize} |
| 1046 | |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 1047 | \item The \module{xmlrpclib} module was contributed to the standard |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 1048 | library by Fredrik Lundh, provding support for writing XML-RPC |
| 1049 | clients. XML-RPC is a simple remote procedure call protocol built on |
Andrew M. Kuchling | 8c69c91 | 2001-08-07 14:28:58 +0000 | [diff] [blame] | 1050 | top of HTTP and XML. For example, the following snippet retrieves a |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 1051 | list of RSS channels from the O'Reilly Network, and then |
| 1052 | lists the recent headlines for one channel: |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 1053 | |
| 1054 | \begin{verbatim} |
| 1055 | import xmlrpclib |
| 1056 | s = xmlrpclib.Server( |
| 1057 | 'http://www.oreillynet.com/meerkat/xml-rpc/server.php') |
| 1058 | channels = s.meerkat.getChannels() |
| 1059 | # channels is a list of dictionaries, like this: |
| 1060 | # [{'id': 4, 'title': 'Freshmeat Daily News'} |
| 1061 | # {'id': 190, 'title': '32Bits Online'}, |
| 1062 | # {'id': 4549, 'title': '3DGamers'}, ... ] |
| 1063 | |
| 1064 | # Get the items for one channel |
| 1065 | items = s.meerkat.getItems( {'channel': 4} ) |
| 1066 | |
| 1067 | # 'items' is another list of dictionaries, like this: |
| 1068 | # [{'link': 'http://freshmeat.net/releases/52719/', |
| 1069 | # 'description': 'A utility which converts HTML to XSL FO.', |
| 1070 | # 'title': 'html2fo 0.3 (Default)'}, ... ] |
| 1071 | \end{verbatim} |
| 1072 | |
Andrew M. Kuchling | d4707e3 | 2001-09-28 20:46:46 +0000 | [diff] [blame] | 1073 | The \module{SimpleXMLRPCServer} module makes it easy to create |
| 1074 | straightforward XML-RPC servers. See \url{http://www.xmlrpc.com/} for |
| 1075 | more information about XML-RPC. |
| 1076 | |
| 1077 | \item The new \module{hmac} module implements implements the HMAC |
| 1078 | algorithm described by \rfc{2104}. |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 1079 | |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 1080 | \item The Python profiler has been extensively reworked and various |
| 1081 | errors in its output have been corrected. (Contributed by Fred |
| 1082 | Fred~L. Drake, Jr. and Tim Peters.) |
| 1083 | |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 1084 | \item The \module{socket} module can be compiled to support IPv6; |
Andrew M. Kuchling | ddeb135 | 2001-07-16 14:35:52 +0000 | [diff] [blame] | 1085 | specify the \longprogramopt{enable-ipv6} option to Python's configure |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 1086 | script. (Contributed by Jun-ichiro ``itojun'' Hagino.) |
| 1087 | |
| 1088 | \item Two new format characters were added to the \module{struct} |
| 1089 | module for 64-bit integers on platforms that support the C |
| 1090 | \ctype{long long} type. \samp{q} is for a signed 64-bit integer, |
| 1091 | and \samp{Q} is for an unsigned one. The value is returned in |
| 1092 | Python's long integer type. (Contributed by Tim Peters.) |
| 1093 | |
| 1094 | \item In the interpreter's interactive mode, there's a new built-in |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 1095 | function \function{help()} that uses the \module{pydoc} module |
| 1096 | introduced in Python 2.1 to provide interactive help. |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 1097 | \code{help(\var{object})} displays any available help text about |
| 1098 | \var{object}. \code{help()} with no argument puts you in an online |
| 1099 | help utility, where you can enter the names of functions, classes, |
| 1100 | or modules to read their help text. |
| 1101 | (Contributed by Guido van Rossum, using Ka-Ping Yee's \module{pydoc} module.) |
| 1102 | |
| 1103 | \item Various bugfixes and performance improvements have been made |
Andrew M. Kuchling | 4cf52a9 | 2001-07-17 12:48:48 +0000 | [diff] [blame] | 1104 | to the SRE engine underlying the \module{re} module. For example, |
Andrew M. Kuchling | beb3855 | 2001-10-22 14:11:06 +0000 | [diff] [blame] | 1105 | the \function{re.sub()} and \function{re.split()} functions have |
| 1106 | been rewritten in C. Another contributed patch speeds up certain |
| 1107 | Unicode character ranges by a factor of two. (SRE is maintained by |
| 1108 | Fredrik Lundh. The BIGCHARSET patch was contributed by Martin von |
| 1109 | L\"owis.) |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 1110 | |
Andrew M. Kuchling | 1efd7ad | 2001-09-14 16:19:27 +0000 | [diff] [blame] | 1111 | \item The \module{smtplib} module now supports \rfc{2487}, ``Secure |
| 1112 | SMTP over TLS'', so it's now possible to encrypt the SMTP traffic |
| 1113 | between a Python program and the mail transport agent being handed a |
| 1114 | message. (Contributed by Gerhard H\"aring.) |
| 1115 | |
Andrew M. Kuchling | a6d2a04 | 2001-07-20 18:34:34 +0000 | [diff] [blame] | 1116 | \item The \module{imaplib} module, maintained by Piers Lauder, has |
| 1117 | support for several new extensions: the NAMESPACE extension defined |
| 1118 | in \rfc{2342}, SORT, GETACL and SETACL. (Contributed by Anthony |
| 1119 | Baxter and Michel Pelletier.) |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 1120 | |
Andrew M. Kuchling | d4707e3 | 2001-09-28 20:46:46 +0000 | [diff] [blame] | 1121 | \item The \module{rfc822} module's parsing of email addresses is now |
| 1122 | compliant with \rfc{2822}, an update to \rfc{822}. (The module's |
| 1123 | name is \emph{not} going to be changed to \samp{rfc2822}.) A new |
| 1124 | package, \module{email}, has also been added for parsing and |
| 1125 | generating e-mail messages. (Contributed by Barry Warsaw, and |
| 1126 | arising out of his work on Mailman.) |
Andrew M. Kuchling | 7770767 | 2001-07-31 15:51:16 +0000 | [diff] [blame] | 1127 | |
| 1128 | \item New constants \constant{ascii_letters}, |
| 1129 | \constant{ascii_lowercase}, and \constant{ascii_uppercase} were |
| 1130 | added to the \module{string} module. There were several modules in |
| 1131 | the standard library that used \constant{string.letters} to mean the |
| 1132 | ranges A-Za-z, but that assumption is incorrect when locales are in |
| 1133 | use, because \constant{string.letters} varies depending on the set |
| 1134 | of legal characters defined by the current locale. The buggy |
| 1135 | modules have all been fixed to use \constant{ascii_letters} instead. |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 1136 | (Reported by an unknown person; fixed by Fred~L. Drake, Jr.) |
Andrew M. Kuchling | 7770767 | 2001-07-31 15:51:16 +0000 | [diff] [blame] | 1137 | |
Andrew M. Kuchling | 8c69c91 | 2001-08-07 14:28:58 +0000 | [diff] [blame] | 1138 | \item The \module{mimetypes} module now makes it easier to use |
| 1139 | alternative MIME-type databases by the addition of a |
| 1140 | \class{MimeTypes} class, which takes a list of filenames to be |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 1141 | parsed. (Contributed by Fred~L. Drake, Jr.) |
Andrew M. Kuchling | 8c69c91 | 2001-08-07 14:28:58 +0000 | [diff] [blame] | 1142 | |
Andrew M. Kuchling | d6e40e2 | 2001-09-10 16:18:50 +0000 | [diff] [blame] | 1143 | \item A \class{Timer} class was added to the \module{threading} |
| 1144 | module that allows scheduling an activity to happen at some future |
| 1145 | time. (Contributed by Itamar Shtull-Trauring.) |
Andrew M. Kuchling | 2f0047a | 2001-09-05 14:53:31 +0000 | [diff] [blame] | 1146 | |
Andrew M. Kuchling | 7770767 | 2001-07-31 15:51:16 +0000 | [diff] [blame] | 1147 | \end{itemize} |
| 1148 | |
| 1149 | |
| 1150 | %====================================================================== |
| 1151 | \section{Interpreter Changes and Fixes} |
| 1152 | |
| 1153 | Some of the changes only affect people who deal with the Python |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 1154 | interpreter at the C level because they're writing Python extension modules, |
Andrew M. Kuchling | 7770767 | 2001-07-31 15:51:16 +0000 | [diff] [blame] | 1155 | embedding the interpreter, or just hacking on the interpreter itself. |
| 1156 | If you only write Python code, none of the changes described here will |
| 1157 | affect you very much. |
| 1158 | |
| 1159 | \begin{itemize} |
| 1160 | |
| 1161 | \item Profiling and tracing functions can now be implemented in C, |
| 1162 | which can operate at much higher speeds than Python-based functions |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 1163 | and should reduce the overhead of profiling and tracing. This |
| 1164 | will be of interest to authors of development environments for |
Andrew M. Kuchling | 7770767 | 2001-07-31 15:51:16 +0000 | [diff] [blame] | 1165 | Python. Two new C functions were added to Python's API, |
| 1166 | \cfunction{PyEval_SetProfile()} and \cfunction{PyEval_SetTrace()}. |
| 1167 | The existing \function{sys.setprofile()} and |
| 1168 | \function{sys.settrace()} functions still exist, and have simply |
| 1169 | been changed to use the new C-level interface. (Contributed by Fred |
| 1170 | L. Drake, Jr.) |
| 1171 | |
| 1172 | \item Another low-level API, primarily of interest to implementors |
| 1173 | of Python debuggers and development tools, was added. |
| 1174 | \cfunction{PyInterpreterState_Head()} and |
| 1175 | \cfunction{PyInterpreterState_Next()} let a caller walk through all |
| 1176 | the existing interpreter objects; |
| 1177 | \cfunction{PyInterpreterState_ThreadHead()} and |
| 1178 | \cfunction{PyThreadState_Next()} allow looping over all the thread |
| 1179 | states for a given interpreter. (Contributed by David Beazley.) |
| 1180 | |
| 1181 | \item A new \samp{et} format sequence was added to |
| 1182 | \cfunction{PyArg_ParseTuple}; \samp{et} takes both a parameter and |
| 1183 | an encoding name, and converts the parameter to the given encoding |
| 1184 | if the parameter turns out to be a Unicode string, or leaves it |
| 1185 | alone if it's an 8-bit string, assuming it to already be in the |
| 1186 | desired encoding. This differs from the \samp{es} format character, |
| 1187 | which assumes that 8-bit strings are in Python's default ASCII |
| 1188 | encoding and converts them to the specified new encoding. |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 1189 | (Contributed by M.-A. Lemburg, and used for the MBCS support on |
| 1190 | Windows described in the following section.) |
Andrew M. Kuchling | cf31d5d | 2001-10-26 20:37:55 +0000 | [diff] [blame] | 1191 | |
| 1192 | \item A different argument parsing function, |
| 1193 | \cfunction{PyArg_UnpackTuple()}, has been added that's simpler and |
| 1194 | presumably faster. Instead of specifying a format string, the |
| 1195 | caller simply gives the minimum and maximum number of arguments |
| 1196 | expected, and a set of pointers to \code{PyObject*} variables that |
| 1197 | will be filled in with argument values. |
| 1198 | |
Andrew M. Kuchling | 0ab31b8 | 2001-08-29 01:16:54 +0000 | [diff] [blame] | 1199 | \item Two new flags \constant{METH_NOARGS} and \constant{METH_O} are |
| 1200 | available in method definition tables to simplify implementation of |
| 1201 | methods with no arguments or a single untyped argument. Calling |
| 1202 | such methods is more efficient than calling a corresponding method |
| 1203 | that uses \constant{METH_VARARGS}. |
| 1204 | Also, the old \constant{METH_OLDARGS} style of writing C methods is |
| 1205 | now officially deprecated. |
| 1206 | |
| 1207 | \item |
| 1208 | Two new wrapper functions, \cfunction{PyOS_snprintf()} and |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 1209 | \cfunction{PyOS_vsnprintf()} were added to provide |
Andrew M. Kuchling | 0ab31b8 | 2001-08-29 01:16:54 +0000 | [diff] [blame] | 1210 | cross-platform implementations for the relatively new |
| 1211 | \cfunction{snprintf()} and \cfunction{vsnprintf()} C lib APIs. In |
| 1212 | contrast to the standard \cfunction{sprintf()} and |
| 1213 | \cfunction{vsprintf()} functions, the Python versions check the |
| 1214 | bounds of the buffer used to protect against buffer overruns. |
| 1215 | (Contributed by M.-A. Lemburg.) |
Andrew M. Kuchling | 7770767 | 2001-07-31 15:51:16 +0000 | [diff] [blame] | 1216 | |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 1217 | \end{itemize} |
| 1218 | |
| 1219 | |
| 1220 | %====================================================================== |
| 1221 | \section{Other Changes and Fixes} |
| 1222 | |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 1223 | % XXX update the patch and bug figures as we go |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 1224 | As usual there were a bunch of other improvements and bugfixes |
| 1225 | scattered throughout the source tree. A search through the CVS change |
Andrew M. Kuchling | 32e3232 | 2001-10-22 15:32:05 +0000 | [diff] [blame] | 1226 | logs finds there were 312 patches applied, and 391 bugs fixed; both |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 1227 | figures are likely to be underestimates. Some of the more notable |
| 1228 | changes are: |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 1229 | |
| 1230 | \begin{itemize} |
| 1231 | |
Andrew M. Kuchling | 0e03f58 | 2001-08-30 21:30:16 +0000 | [diff] [blame] | 1232 | \item The code for the MacOS port for Python, maintained by Jack |
| 1233 | Jansen, is now kept in the main Python CVS tree, and many changes |
Andrew M. Kuchling | 279e744 | 2001-10-22 02:03:40 +0000 | [diff] [blame] | 1234 | have been made to support MacOS~X. |
Andrew M. Kuchling | 0e03f58 | 2001-08-30 21:30:16 +0000 | [diff] [blame] | 1235 | |
| 1236 | The most significant change is the ability to build Python as a |
| 1237 | framework, enabled by supplying the \longprogramopt{enable-framework} |
| 1238 | option to the configure script when compiling Python. According to |
| 1239 | Jack Jansen, ``This installs a self-contained Python installation plus |
Andrew M. Kuchling | 279e744 | 2001-10-22 02:03:40 +0000 | [diff] [blame] | 1240 | the OS~X framework "glue" into |
Andrew M. Kuchling | 0e03f58 | 2001-08-30 21:30:16 +0000 | [diff] [blame] | 1241 | \file{/Library/Frameworks/Python.framework} (or another location of |
| 1242 | choice). For now there is little immediate added benefit to this |
| 1243 | (actually, there is the disadvantage that you have to change your PATH |
| 1244 | to be able to find Python), but it is the basis for creating a |
| 1245 | full-blown Python application, porting the MacPython IDE, possibly |
| 1246 | using Python as a standard OSA scripting language and much more.'' |
| 1247 | |
| 1248 | Most of the MacPython toolbox modules, which interface to MacOS APIs |
Andrew M. Kuchling | 279e744 | 2001-10-22 02:03:40 +0000 | [diff] [blame] | 1249 | such as windowing, QuickTime, scripting, etc. have been ported to OS~X, |
Andrew M. Kuchling | beb3855 | 2001-10-22 14:11:06 +0000 | [diff] [blame] | 1250 | but they've been left commented out in \file{setup.py}. People who want |
Andrew M. Kuchling | 0e03f58 | 2001-08-30 21:30:16 +0000 | [diff] [blame] | 1251 | to experiment with these modules can uncomment them manually. |
| 1252 | |
| 1253 | % Jack's original comments: |
| 1254 | %The main change is the possibility to build Python as a |
| 1255 | %framework. This installs a self-contained Python installation plus the |
| 1256 | %OSX framework "glue" into /Library/Frameworks/Python.framework (or |
| 1257 | %another location of choice). For now there is little immedeate added |
| 1258 | %benefit to this (actually, there is the disadvantage that you have to |
| 1259 | %change your PATH to be able to find Python), but it is the basis for |
| 1260 | %creating a fullblown Python application, porting the MacPython IDE, |
| 1261 | %possibly using Python as a standard OSA scripting language and much |
| 1262 | %more. You enable this with "configure --enable-framework". |
| 1263 | |
| 1264 | %The other change is that most MacPython toolbox modules, which |
| 1265 | %interface to all the MacOS APIs such as windowing, quicktime, |
| 1266 | %scripting, etc. have been ported. Again, most of these are not of |
| 1267 | %immedeate use, as they need a full application to be really useful, so |
| 1268 | %they have been commented out in setup.py. People wanting to experiment |
| 1269 | %can uncomment them. Gestalt and Internet Config modules are enabled by |
| 1270 | %default. |
Andrew M. Kuchling | 0e03f58 | 2001-08-30 21:30:16 +0000 | [diff] [blame] | 1271 | |
Andrew M. Kuchling | 2cd712b | 2001-07-16 13:39:08 +0000 | [diff] [blame] | 1272 | \item Keyword arguments passed to builtin functions that don't take them |
| 1273 | now cause a \exception{TypeError} exception to be raised, with the |
| 1274 | message "\var{function} takes no keyword arguments". |
| 1275 | |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 1276 | \item Weak references, added in Python 2.1 as an extension module, |
| 1277 | are now part of the core because they're used in the implementation |
| 1278 | of new-style classes. The \exception{ReferenceError} exception has |
| 1279 | therefore moved from the \module{weakref} module to become a |
| 1280 | built-in exception. |
| 1281 | |
Andrew M. Kuchling | 94a7eba | 2001-08-15 15:55:48 +0000 | [diff] [blame] | 1282 | \item A new script, \file{Tools/scripts/cleanfuture.py} by Tim |
| 1283 | Peters, automatically removes obsolete \code{__future__} statements |
| 1284 | from Python source code. |
Andrew M. Kuchling | 2cd712b | 2001-07-16 13:39:08 +0000 | [diff] [blame] | 1285 | |
Andrew M. Kuchling | 4f9e220 | 2001-10-29 18:09:42 +0000 | [diff] [blame] | 1286 | \item An additional \var{flags} argument has been added to the |
| 1287 | built-in function \function{compile()}, so the behaviour of |
| 1288 | \code{__future__} statements can now be correctly observed in |
| 1289 | simulated shells, such as those presented by IDLE and other |
| 1290 | development environments. This is described in \pep{264}. |
| 1291 | (Contributed by Michael Hudson.) |
| 1292 | |
Andrew M. Kuchling | 2cd712b | 2001-07-16 13:39:08 +0000 | [diff] [blame] | 1293 | \item The new license introduced with Python 1.6 wasn't |
| 1294 | GPL-compatible. This is fixed by some minor textual changes to the |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 1295 | 2.2 license, so it's now legal to embed Python inside a GPLed |
| 1296 | program again. Note that Python itself is not GPLed, but instead is |
| 1297 | under a license that's essentially equivalent to the BSD license, |
| 1298 | same as it always was. The license changes were also applied to the |
| 1299 | Python 2.0.1 and 2.1.1 releases. |
Andrew M. Kuchling | 2cd712b | 2001-07-16 13:39:08 +0000 | [diff] [blame] | 1300 | |
Andrew M. Kuchling | f4ccf58 | 2001-07-31 01:11:36 +0000 | [diff] [blame] | 1301 | \item When presented with a Unicode filename on Windows, Python will |
| 1302 | now convert it to an MBCS encoded string, as used by the Microsoft |
| 1303 | file APIs. As MBCS is explicitly used by the file APIs, Python's |
| 1304 | choice of ASCII as the default encoding turns out to be an |
| 1305 | annoyance. |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 1306 | (Contributed by Mark Hammond with assistance from Marc-Andr\'e |
| 1307 | Lemburg.) |
| 1308 | |
Andrew M. Kuchling | d6e40e2 | 2001-09-10 16:18:50 +0000 | [diff] [blame] | 1309 | \item Large file support is now enabled on Windows. (Contributed by |
| 1310 | Tim Peters.) |
| 1311 | |
Andrew M. Kuchling | 2cd712b | 2001-07-16 13:39:08 +0000 | [diff] [blame] | 1312 | \item The \file{Tools/scripts/ftpmirror.py} script |
| 1313 | now parses a \file{.netrc} file, if you have one. |
Andrew M. Kuchling | 4cf52a9 | 2001-07-17 12:48:48 +0000 | [diff] [blame] | 1314 | (Contributed by Mike Romberg.) |
Andrew M. Kuchling | 2cd712b | 2001-07-16 13:39:08 +0000 | [diff] [blame] | 1315 | |
Andrew M. Kuchling | 4cf52a9 | 2001-07-17 12:48:48 +0000 | [diff] [blame] | 1316 | \item Some features of the object returned by the |
| 1317 | \function{xrange()} function are now deprecated, and trigger |
| 1318 | warnings when they're accessed; they'll disappear in Python 2.3. |
| 1319 | \class{xrange} objects tried to pretend they were full sequence |
| 1320 | types by supporting slicing, sequence multiplication, and the |
| 1321 | \keyword{in} operator, but these features were rarely used and |
| 1322 | therefore buggy. The \method{tolist()} method and the |
| 1323 | \member{start}, \member{stop}, and \member{step} attributes are also |
| 1324 | being deprecated. At the C level, the fourth argument to the |
| 1325 | \cfunction{PyRange_New()} function, \samp{repeat}, has also been |
| 1326 | deprecated. |
| 1327 | |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 1328 | \item There were a bunch of patches to the dictionary |
| 1329 | implementation, mostly to fix potential core dumps if a dictionary |
| 1330 | contains objects that sneakily changed their hash value, or mutated |
| 1331 | the dictionary they were contained in. For a while python-dev fell |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame] | 1332 | into a gentle rhythm of Michael Hudson finding a case that dumped |
| 1333 | core, Tim Peters fixing the bug, Michael finding another case, and round |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 1334 | and round it went. |
| 1335 | |
Andrew M. Kuchling | 33a3b63 | 2001-09-04 21:25:58 +0000 | [diff] [blame] | 1336 | \item On Windows, Python can now be compiled with Borland C thanks |
| 1337 | to a number of patches contributed by Stephen Hansen, though the |
| 1338 | result isn't fully functional yet. (But this \emph{is} progress...) |
Andrew M. Kuchling | 8c69c91 | 2001-08-07 14:28:58 +0000 | [diff] [blame] | 1339 | |
Andrew M. Kuchling | f4ccf58 | 2001-07-31 01:11:36 +0000 | [diff] [blame] | 1340 | \item Another Windows enhancement: Wise Solutions generously offered |
| 1341 | PythonLabs use of their InstallerMaster 8.1 system. Earlier |
| 1342 | PythonLabs Windows installers used Wise 5.0a, which was beginning to |
| 1343 | show its age. (Packaged up by Tim Peters.) |
| 1344 | |
Andrew M. Kuchling | 8c69c91 | 2001-08-07 14:28:58 +0000 | [diff] [blame] | 1345 | \item Files ending in \samp{.pyw} can now be imported on Windows. |
| 1346 | \samp{.pyw} is a Windows-only thing, used to indicate that a script |
| 1347 | needs to be run using PYTHONW.EXE instead of PYTHON.EXE in order to |
| 1348 | prevent a DOS console from popping up to display the output. This |
| 1349 | patch makes it possible to import such scripts, in case they're also |
| 1350 | usable as modules. (Implemented by David Bolen.) |
| 1351 | |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 1352 | \item On platforms where Python uses the C \cfunction{dlopen()} function |
| 1353 | to load extension modules, it's now possible to set the flags used |
| 1354 | by \cfunction{dlopen()} using the \function{sys.getdlopenflags()} and |
| 1355 | \function{sys.setdlopenflags()} functions. (Contributed by Bram Stolk.) |
Andrew M. Kuchling | 2f0047a | 2001-09-05 14:53:31 +0000 | [diff] [blame] | 1356 | |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 1357 | \item The \function{pow()} built-in function no longer supports 3 |
| 1358 | arguments when floating-point numbers are supplied. |
Andrew M. Kuchling | 1497b62 | 2001-09-24 14:51:16 +0000 | [diff] [blame] | 1359 | \code{pow(\var{x}, \var{y}, \var{z})} returns \code{(x**y) \% z}, but |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 1360 | this is never useful for floating point numbers, and the final |
| 1361 | result varies unpredictably depending on the platform. A call such |
Andrew M. Kuchling | d6e40e2 | 2001-09-10 16:18:50 +0000 | [diff] [blame] | 1362 | as \code{pow(2.0, 8.0, 7.0)} will now raise a \exception{TypeError} |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 1363 | exception. |
Andrew M. Kuchling | 7770767 | 2001-07-31 15:51:16 +0000 | [diff] [blame] | 1364 | |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 1365 | \end{itemize} |
| 1366 | |
| 1367 | |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 1368 | %====================================================================== |
| 1369 | \section{Acknowledgements} |
| 1370 | |
| 1371 | The author would like to thank the following people for offering |
Andrew M. Kuchling | b83769c | 2001-10-26 20:07:03 +0000 | [diff] [blame] | 1372 | suggestions, corrections and assistance with various drafts of this |
| 1373 | article: Fred Bremmer, Keith Briggs, Andrew Dalke, Fred~L. Drake, Jr., |
Andrew M. Kuchling | 2836907 | 2001-10-29 15:47:33 +0000 | [diff] [blame] | 1374 | Carel Fellinger, Mark Hammond, Stephen Hansen, Michael Hudson, Jack Jansen, |
Andrew M. Kuchling | 7cc13de | 2001-10-30 14:22:11 +0000 | [diff] [blame] | 1375 | Marc-Andr\'e Lemburg, Fredrik Lundh, Michael McLay, Tim Peters, Jens |
| 1376 | Quade, Tom Reinhardt, Neil Schemenauer, Guido van Rossum. |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 1377 | |
| 1378 | \end{document} |