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 | ab01087 | 2001-07-19 14:59:53 +0000 | [diff] [blame] | 6 | \release{0.04} |
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 |
| 16 | for Python 2.2 alpha 1. Please send any comments, bug reports, or |
| 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 | 7bf8277 | 2001-07-11 18:54:26 +0000 | [diff] [blame] | 20 | This article explains the new features in Python 2.2. Python 2.2 |
| 21 | includes some significant changes that go far toward cleaning up the |
| 22 | language's darkest corners, and some exciting new features. |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 23 | |
| 24 | This article doesn't attempt to provide a complete specification for |
| 25 | the new features, but instead provides a convenient overview of the |
| 26 | new features. For full details, you should refer to 2.2 documentation |
Fred Drake | 0d00254 | 2001-07-17 13:55:33 +0000 | [diff] [blame] | 27 | such as the |
| 28 | \citetitle[http://python.sourceforge.net/devel-docs/lib/lib.html]{Python |
| 29 | Library Reference} and the |
| 30 | \citetitle[http://python.sourceforge.net/devel-docs/ref/ref.html]{Python |
| 31 | Reference Manual}, or to the PEP for a particular new feature. |
| 32 | % These \citetitle marks should get the python.org URLs for the final |
| 33 | % release, just as soon as the docs are published there. |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 34 | |
| 35 | The final release of Python 2.2 is planned for October 2001. |
| 36 | |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 37 | |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 38 | %====================================================================== |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 39 | % It looks like this set of changes will likely get into 2.2, |
| 40 | % so I need to read and digest the relevant PEPs. |
Andrew M. Kuchling | 7bf8277 | 2001-07-11 18:54:26 +0000 | [diff] [blame] | 41 | %\section{PEP 252: Type and Class Changes} |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 42 | |
Andrew M. Kuchling | 7bf8277 | 2001-07-11 18:54:26 +0000 | [diff] [blame] | 43 | %XXX |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 44 | |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 45 | % GvR's description at http://www.python.org/2.2/descrintro.html |
| 46 | |
Andrew M. Kuchling | 7bf8277 | 2001-07-11 18:54:26 +0000 | [diff] [blame] | 47 | %\begin{seealso} |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 48 | |
Andrew M. Kuchling | 7bf8277 | 2001-07-11 18:54:26 +0000 | [diff] [blame] | 49 | %\seepep{252}{Making Types Look More Like Classes}{Written and implemented |
| 50 | %by GvR.} |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 51 | |
Andrew M. Kuchling | 7bf8277 | 2001-07-11 18:54:26 +0000 | [diff] [blame] | 52 | %\end{seealso} |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 53 | |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 54 | |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 55 | %====================================================================== |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 56 | \section{PEP 234: Iterators} |
| 57 | |
| 58 | A significant addition to 2.2 is an iteration interface at both the C |
| 59 | and Python levels. Objects can define how they can be looped over by |
| 60 | callers. |
| 61 | |
| 62 | In Python versions up to 2.1, the usual way to make \code{for item in |
| 63 | obj} work is to define a \method{__getitem__()} method that looks |
| 64 | something like this: |
| 65 | |
| 66 | \begin{verbatim} |
| 67 | def __getitem__(self, index): |
| 68 | return <next item> |
| 69 | \end{verbatim} |
| 70 | |
| 71 | \method{__getitem__()} is more properly used to define an indexing |
| 72 | operation on an object so that you can write \code{obj[5]} to retrieve |
Andrew M. Kuchling | 8c69c91b | 2001-08-07 14:28:58 +0000 | [diff] [blame] | 73 | 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] | 74 | to support \keyword{for} loops. Consider some file-like object that |
| 75 | wants to be looped over; the \var{index} parameter is essentially |
| 76 | meaningless, as the class probably assumes that a series of |
| 77 | \method{__getitem__()} calls will be made, with \var{index} |
| 78 | incrementing by one each time. In other words, the presence of the |
| 79 | \method{__getitem__()} method doesn't mean that \code{file[5]} will |
| 80 | work, though it really should. |
| 81 | |
| 82 | In Python 2.2, iteration can be implemented separately, and |
| 83 | \method{__getitem__()} methods can be limited to classes that really |
| 84 | do support random access. The basic idea of iterators is quite |
| 85 | simple. A new built-in function, \function{iter(obj)}, returns an |
| 86 | iterator for the object \var{obj}. (It can also take two arguments: |
Fred Drake | 0d00254 | 2001-07-17 13:55:33 +0000 | [diff] [blame] | 87 | \code{iter(\var{C}, \var{sentinel})} will call the callable \var{C}, |
| 88 | until it returns \var{sentinel}, which will signal that the iterator |
| 89 | is done. This form probably won't be used very often.) |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 90 | |
| 91 | Python classes can define an \method{__iter__()} method, which should |
| 92 | create and return a new iterator for the object; if the object is its |
| 93 | own iterator, this method can just return \code{self}. In particular, |
| 94 | iterators will usually be their own iterators. Extension types |
| 95 | 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] | 96 | return an iterator, and extension types that want to behave as |
| 97 | iterators can define a \code{tp_iternext} function. |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 98 | |
| 99 | So what do iterators do? They have one required method, |
| 100 | \method{next()}, which takes no arguments and returns the next value. |
| 101 | When there are no more values to be returned, calling \method{next()} |
| 102 | should raise the \exception{StopIteration} exception. |
| 103 | |
| 104 | \begin{verbatim} |
| 105 | >>> L = [1,2,3] |
| 106 | >>> i = iter(L) |
| 107 | >>> print i |
| 108 | <iterator object at 0x8116870> |
| 109 | >>> i.next() |
| 110 | 1 |
| 111 | >>> i.next() |
| 112 | 2 |
| 113 | >>> i.next() |
| 114 | 3 |
| 115 | >>> i.next() |
| 116 | Traceback (most recent call last): |
| 117 | File "<stdin>", line 1, in ? |
| 118 | StopIteration |
| 119 | >>> |
| 120 | \end{verbatim} |
| 121 | |
| 122 | In 2.2, Python's \keyword{for} statement no longer expects a sequence; |
| 123 | it expects something for which \function{iter()} will return something. |
| 124 | For backward compatibility, and convenience, an iterator is |
| 125 | automatically constructed for sequences that don't implement |
| 126 | \method{__iter__()} or a \code{tp_iter} slot, so \code{for i in |
| 127 | [1,2,3]} will still work. Wherever the Python interpreter loops over |
| 128 | a sequence, it's been changed to use the iterator protocol. This |
| 129 | means you can do things like this: |
| 130 | |
| 131 | \begin{verbatim} |
| 132 | >>> i = iter(L) |
| 133 | >>> a,b,c = i |
| 134 | >>> a,b,c |
| 135 | (1, 2, 3) |
| 136 | >>> |
| 137 | \end{verbatim} |
| 138 | |
Andrew M. Kuchling | 9e9c135 | 2001-08-11 03:06:50 +0000 | [diff] [blame] | 139 | Iterator support has been added to some of Python's basic types. |
Fred Drake | 0d00254 | 2001-07-17 13:55:33 +0000 | [diff] [blame] | 140 | Calling \function{iter()} on a dictionary will return an iterator |
Andrew M. Kuchling | 6ea9f0b | 2001-07-17 14:50:31 +0000 | [diff] [blame] | 141 | which loops over its keys: |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 142 | |
| 143 | \begin{verbatim} |
| 144 | >>> m = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6, |
| 145 | ... 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12} |
| 146 | >>> for key in m: print key, m[key] |
| 147 | ... |
| 148 | Mar 3 |
| 149 | Feb 2 |
| 150 | Aug 8 |
| 151 | Sep 9 |
| 152 | May 5 |
| 153 | Jun 6 |
| 154 | Jul 7 |
| 155 | Jan 1 |
| 156 | Apr 4 |
| 157 | Nov 11 |
| 158 | Dec 12 |
| 159 | Oct 10 |
| 160 | >>> |
| 161 | \end{verbatim} |
| 162 | |
| 163 | That's just the default behaviour. If you want to iterate over keys, |
| 164 | values, or key/value pairs, you can explicitly call the |
| 165 | \method{iterkeys()}, \method{itervalues()}, or \method{iteritems()} |
Andrew M. Kuchling | 9e9c135 | 2001-08-11 03:06:50 +0000 | [diff] [blame] | 166 | methods to get an appropriate iterator. In a minor related change, |
| 167 | the \keyword{in} operator now works on dictionaries, so |
| 168 | \code{\var{key} in dict} is now equivalent to |
| 169 | \code{dict.has_key(\var{key})}. |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 170 | |
Andrew M. Kuchling | 9e9c135 | 2001-08-11 03:06:50 +0000 | [diff] [blame] | 171 | |
| 172 | Files also provide an iterator, which calls the \method{readline()} |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 173 | method until there are no more lines in the file. This means you can |
| 174 | now read each line of a file using code like this: |
| 175 | |
| 176 | \begin{verbatim} |
| 177 | for line in file: |
| 178 | # do something for each line |
| 179 | \end{verbatim} |
| 180 | |
| 181 | Note that you can only go forward in an iterator; there's no way to |
| 182 | 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] | 183 | An iterator object could provide such additional capabilities, but the |
| 184 | iterator protocol only requires a \method{next()} method. |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 185 | |
| 186 | \begin{seealso} |
| 187 | |
| 188 | \seepep{234}{Iterators}{Written by Ka-Ping Yee and GvR; implemented |
| 189 | by the Python Labs crew, mostly by GvR and Tim Peters.} |
| 190 | |
| 191 | \end{seealso} |
| 192 | |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 193 | |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 194 | %====================================================================== |
| 195 | \section{PEP 255: Simple Generators} |
| 196 | |
| 197 | Generators are another new feature, one that interacts with the |
| 198 | introduction of iterators. |
| 199 | |
| 200 | You're doubtless familiar with how function calls work in Python or |
| 201 | C. When you call a function, it gets a private area where its local |
| 202 | variables are created. When the function reaches a \keyword{return} |
| 203 | statement, the local variables are destroyed and the resulting value |
| 204 | is returned to the caller. A later call to the same function will get |
| 205 | a fresh new set of local variables. But, what if the local variables |
| 206 | weren't destroyed on exiting a function? What if you could later |
| 207 | resume the function where it left off? This is what generators |
| 208 | provide; they can be thought of as resumable functions. |
| 209 | |
| 210 | Here's the simplest example of a generator function: |
| 211 | |
| 212 | \begin{verbatim} |
| 213 | def generate_ints(N): |
| 214 | for i in range(N): |
| 215 | yield i |
| 216 | \end{verbatim} |
| 217 | |
| 218 | A new keyword, \keyword{yield}, was introduced for generators. Any |
| 219 | function containing a \keyword{yield} statement is a generator |
| 220 | function; this is detected by Python's bytecode compiler which |
Andrew M. Kuchling | 4cf52a9 | 2001-07-17 12:48:48 +0000 | [diff] [blame] | 221 | compiles the function specially. Because a new keyword was |
| 222 | introduced, generators must be explicitly enabled in a module by |
| 223 | including a \code{from __future__ import generators} statement near |
| 224 | the top of the module's source code. In Python 2.3 this statement |
| 225 | will become unnecessary. |
| 226 | |
| 227 | When you call a generator function, it doesn't return a single value; |
| 228 | instead it returns a generator object that supports the iterator |
| 229 | interface. On executing the \keyword{yield} statement, the generator |
| 230 | outputs the value of \code{i}, similar to a \keyword{return} |
| 231 | statement. The big difference between \keyword{yield} and a |
| 232 | \keyword{return} statement is that, on reaching a \keyword{yield} the |
| 233 | generator's state of execution is suspended and local variables are |
| 234 | preserved. On the next call to the generator's \code{.next()} method, |
| 235 | the function will resume executing immediately after the |
| 236 | \keyword{yield} statement. (For complicated reasons, the |
| 237 | \keyword{yield} statement isn't allowed inside the \keyword{try} block |
| 238 | of a \code{try...finally} statement; read PEP 255 for a full |
| 239 | explanation of the interaction between \keyword{yield} and |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 240 | exceptions.) |
| 241 | |
| 242 | Here's a sample usage of the \function{generate_ints} generator: |
| 243 | |
| 244 | \begin{verbatim} |
| 245 | >>> gen = generate_ints(3) |
| 246 | >>> gen |
| 247 | <generator object at 0x8117f90> |
| 248 | >>> gen.next() |
| 249 | 0 |
| 250 | >>> gen.next() |
| 251 | 1 |
| 252 | >>> gen.next() |
| 253 | 2 |
| 254 | >>> gen.next() |
| 255 | Traceback (most recent call last): |
| 256 | File "<stdin>", line 1, in ? |
| 257 | File "<stdin>", line 2, in generate_ints |
| 258 | StopIteration |
| 259 | >>> |
| 260 | \end{verbatim} |
| 261 | |
| 262 | You could equally write \code{for i in generate_ints(5)}, or |
| 263 | \code{a,b,c = generate_ints(3)}. |
| 264 | |
| 265 | Inside a generator function, the \keyword{return} statement can only |
Andrew M. Kuchling | 4cf52a9 | 2001-07-17 12:48:48 +0000 | [diff] [blame] | 266 | be used without a value, and signals the end of the procession of |
| 267 | values; afterwards the generator cannot return any further values. |
| 268 | \keyword{return} with a value, such as \code{return 5}, is a syntax |
| 269 | error inside a generator function. The end of the generator's results |
| 270 | can also be indicated by raising \exception{StopIteration} manually, |
| 271 | or by just letting the flow of execution fall off the bottom of the |
| 272 | function. |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 273 | |
| 274 | You could achieve the effect of generators manually by writing your |
Andrew M. Kuchling | 4cf52a9 | 2001-07-17 12:48:48 +0000 | [diff] [blame] | 275 | 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] | 276 | instance variables. For example, returning a list of integers could |
| 277 | be done by setting \code{self.count} to 0, and having the |
| 278 | \method{next()} method increment \code{self.count} and return it. |
Andrew M. Kuchling | c32cc7c | 2001-07-17 18:25:01 +0000 | [diff] [blame] | 279 | However, for a moderately complicated generator, writing a |
| 280 | corresponding class would be much messier. |
| 281 | \file{Lib/test/test_generators.py} contains a number of more |
| 282 | interesting examples. The simplest one implements an in-order |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 283 | traversal of a tree using generators recursively. |
| 284 | |
| 285 | \begin{verbatim} |
| 286 | # A recursive generator that generates Tree leaves in in-order. |
| 287 | def inorder(t): |
| 288 | if t: |
| 289 | for x in inorder(t.left): |
| 290 | yield x |
| 291 | yield t.label |
| 292 | for x in inorder(t.right): |
| 293 | yield x |
| 294 | \end{verbatim} |
| 295 | |
| 296 | Two other examples in \file{Lib/test/test_generators.py} produce |
| 297 | solutions for the N-Queens problem (placing $N$ queens on an $NxN$ |
| 298 | chess board so that no queen threatens another) and the Knight's Tour |
| 299 | (a route that takes a knight to every square of an $NxN$ chessboard |
| 300 | without visiting any square twice). |
| 301 | |
| 302 | The idea of generators comes from other programming languages, |
| 303 | especially Icon (\url{http://www.cs.arizona.edu/icon/}), where the |
| 304 | idea of generators is central to the language. In Icon, every |
| 305 | expression and function call behaves like a generator. One example |
| 306 | from ``An Overview of the Icon Programming Language'' at |
| 307 | \url{http://www.cs.arizona.edu/icon/docs/ipd266.htm} gives an idea of |
| 308 | what this looks like: |
| 309 | |
| 310 | \begin{verbatim} |
| 311 | sentence := "Store it in the neighboring harbor" |
| 312 | if (i := find("or", sentence)) > 5 then write(i) |
| 313 | \end{verbatim} |
| 314 | |
| 315 | The \function{find()} function returns the indexes at which the |
| 316 | substring ``or'' is found: 3, 23, 33. In the \keyword{if} statement, |
| 317 | \code{i} is first assigned a value of 3, but 3 is less than 5, so the |
| 318 | comparison fails, and Icon retries it with the second value of 23. 23 |
| 319 | is greater than 5, so the comparison now succeeds, and the code prints |
| 320 | the value 23 to the screen. |
| 321 | |
| 322 | Python doesn't go nearly as far as Icon in adopting generators as a |
| 323 | central concept. Generators are considered a new part of the core |
| 324 | Python language, but learning or using them isn't compulsory; if they |
| 325 | don't solve any problems that you have, feel free to ignore them. |
| 326 | This is different from Icon where the idea of generators is a basic |
| 327 | concept. One novel feature of Python's interface as compared to |
| 328 | Icon's is that a generator's state is represented as a concrete object |
| 329 | that can be passed around to other functions or stored in a data |
| 330 | structure. |
| 331 | |
| 332 | \begin{seealso} |
| 333 | |
Andrew M. Kuchling | 4cf52a9 | 2001-07-17 12:48:48 +0000 | [diff] [blame] | 334 | \seepep{255}{Simple Generators}{Written by Neil Schemenauer, Tim |
| 335 | Peters, Magnus Lie Hetland. Implemented mostly by Neil Schemenauer |
| 336 | and Tim Peters, with other fixes from the Python Labs crew.} |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 337 | |
| 338 | \end{seealso} |
| 339 | |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 340 | |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 341 | %====================================================================== |
Andrew M. Kuchling | 9e9c135 | 2001-08-11 03:06:50 +0000 | [diff] [blame] | 342 | \section{PEP 238: Changing the Division Operator} |
| 343 | |
| 344 | The most controversial change in Python 2.2 is the start of an effort |
| 345 | to fix an old design flaw that's been in Python from the beginning. |
| 346 | Currently Python's division operator, \code{/}, behaves like C's |
| 347 | division operator when presented with two integer arguments. It |
| 348 | returns an integer result that's truncated down when there would be |
| 349 | fractional part. For example, \code{3/2} is 1, not 1.5, and |
| 350 | \code{(-1)/2} is -1, not -0.5. This means that the results of divison |
| 351 | can vary unexpectedly depending on the type of the two operands and |
| 352 | because Python is dynamically typed, it can be difficult to determine |
| 353 | the possible types of the operands. |
| 354 | |
| 355 | (The controversy is over whether this is \emph{really} a design flaw, |
| 356 | and whether it's worth breaking existing code to fix this. It's |
| 357 | caused endless discussions on python-dev and in July erupted into an |
| 358 | storm of acidly sarcastic postings on \newsgroup{comp.lang.python}. I |
| 359 | won't argue for either side here; read PEP 238 for a summary of |
| 360 | arguments and counter-arguments.) |
| 361 | |
| 362 | Because this change might break code, it's being introduced very |
| 363 | gradually. Python 2.2 begins the transition, but the switch won't be |
| 364 | complete until Python 3.0. |
| 365 | |
| 366 | First, some terminology from PEP 238. ``True division'' is the |
| 367 | division that most non-programmers are familiar with: 3/2 is 1.5, 1/4 |
| 368 | is 0.25, and so forth. ``Floor division'' is what Python's \code{/} |
| 369 | operator currently does when given integer operands; the result is the |
| 370 | floor of the value returned by true division. ``Classic division'' is |
| 371 | the current mixed behaviour of \code{/}; it returns the result of |
| 372 | floor division when the operands are integers, and returns the result |
| 373 | of true division when one of the operands is a floating-point number. |
| 374 | |
| 375 | Here are the changes 2.2 introduces: |
| 376 | |
| 377 | \begin{itemize} |
| 378 | |
| 379 | \item A new operator, \code{//}, is the floor division operator. |
| 380 | (Yes, we know it looks like \Cpp's comment symbol.) \code{//} |
| 381 | \emph{always} returns the floor divison no matter what the types of |
| 382 | its operands are, so \code{1 // 2} is 0 and \code{1.0 // 2.0} is also |
| 383 | 0.0. |
| 384 | |
| 385 | \code{//} is always available in Python 2.2; you don't need to enable |
| 386 | it using a \code{__future__} statement. |
| 387 | |
| 388 | \item By including a \code{from __future__ import true_division} in a |
| 389 | module, the \code{/} operator will be changed to return the result of |
| 390 | true division, so \code{1/2} is 0.5. Without the \code{__future__} |
| 391 | statement, \code{/} still means classic division. The default meaning |
| 392 | of \code{/} will not change until Python 3.0. |
| 393 | |
| 394 | \item Classes can define methods called \method{__truediv__} and |
| 395 | \method{__floordiv__} to overload the two division operators. At the |
| 396 | C level, there are also slots in the \code{PyNumberMethods} structure |
| 397 | so extension types can define the two operators. |
| 398 | |
| 399 | % XXX a warning someday? |
| 400 | |
| 401 | \end{itemize} |
| 402 | |
| 403 | \begin{seealso} |
| 404 | |
| 405 | \seepep{238}{Changing the Division Operator}{Written by Moshe Zadka and |
| 406 | Guido van Rossum. Implemented by Guido van Rossum..} |
| 407 | |
| 408 | \end{seealso} |
| 409 | |
| 410 | |
| 411 | %====================================================================== |
Andrew M. Kuchling | a43e703 | 2001-06-27 20:32:12 +0000 | [diff] [blame] | 412 | \section{Unicode Changes} |
| 413 | |
Andrew M. Kuchling | 2cd712b | 2001-07-16 13:39:08 +0000 | [diff] [blame] | 414 | 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] | 415 | 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] | 416 | Python 2.2 can also be compiled to use UCS-4, 32-bit unsigned |
| 417 | integers, as its internal encoding by supplying |
| 418 | \longprogramopt{enable-unicode=ucs4} to the configure script. When |
Andrew M. Kuchling | ab01087 | 2001-07-19 14:59:53 +0000 | [diff] [blame] | 419 | 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] | 420 | handle Unicode characters from U+000000 to U+110000, so the range of |
| 421 | legal values for the \function{unichr()} function is expanded |
| 422 | accordingly. Using an interpreter compiled to use UCS-2 (a ``narrow |
| 423 | Python''), values greater than 65535 will still cause |
| 424 | \function{unichr()} to raise a \exception{ValueError} exception. |
Andrew M. Kuchling | ab01087 | 2001-07-19 14:59:53 +0000 | [diff] [blame] | 425 | |
| 426 | All this is the province of the still-unimplemented PEP 261, ``Support |
| 427 | for `wide' Unicode characters''; consult it for further details, and |
Andrew M. Kuchling | a6d2a04 | 2001-07-20 18:34:34 +0000 | [diff] [blame] | 428 | please offer comments on the PEP and on your experiences with the |
| 429 | 2.2 alpha releases. |
| 430 | % XXX update previous line once 2.2 reaches beta. |
Andrew M. Kuchling | ab01087 | 2001-07-19 14:59:53 +0000 | [diff] [blame] | 431 | |
| 432 | Another change is much simpler to explain. Since their introduction, |
| 433 | Unicode strings have supported an \method{encode()} method to convert |
| 434 | the string to a selected encoding such as UTF-8 or Latin-1. A |
| 435 | symmetric \method{decode(\optional{\var{encoding}})} method has been |
| 436 | added to 8-bit strings (though not to Unicode strings) in 2.2. |
| 437 | \method{decode()} assumes that the string is in the specified encoding |
| 438 | and decodes it, returning whatever is returned by the codec. |
| 439 | |
| 440 | Using this new feature, codecs have been added for tasks not directly |
| 441 | related to Unicode. For example, codecs have been added for |
| 442 | uu-encoding, MIME's base64 encoding, and compression with the |
| 443 | \module{zlib} module: |
Andrew M. Kuchling | 2cd712b | 2001-07-16 13:39:08 +0000 | [diff] [blame] | 444 | |
| 445 | \begin{verbatim} |
| 446 | >>> s = """Here is a lengthy piece of redundant, overly verbose, |
| 447 | ... and repetitive text. |
| 448 | ... """ |
| 449 | >>> data = s.encode('zlib') |
| 450 | >>> data |
| 451 | 'x\x9c\r\xc9\xc1\r\x80 \x10\x04\xc0?Ul...' |
| 452 | >>> data.decode('zlib') |
| 453 | 'Here is a lengthy piece of redundant, overly verbose,\nand repetitive text.\n' |
| 454 | >>> print s.encode('uu') |
| 455 | begin 666 <data> |
| 456 | M2&5R92!I<R!A(&QE;F=T:'D@<&EE8V4@;V8@<F5D=6YD86YT+"!O=F5R;'D@ |
| 457 | >=F5R8F]S92P*86YD(')E<&5T:71I=F4@=&5X="X* |
| 458 | |
| 459 | end |
| 460 | >>> "sheesh".encode('rot-13') |
| 461 | 'furrfu' |
| 462 | \end{verbatim} |
Andrew M. Kuchling | a43e703 | 2001-06-27 20:32:12 +0000 | [diff] [blame] | 463 | |
Andrew M. Kuchling | f5fec3c | 2001-07-19 01:48:08 +0000 | [diff] [blame] | 464 | \method{encode()} and \method{decode()} were implemented by |
| 465 | Marc-Andr\'e Lemburg. The changes to support using UCS-4 internally |
| 466 | were implemented by Fredrik Lundh and Martin von L\"owis. |
Andrew M. Kuchling | a43e703 | 2001-06-27 20:32:12 +0000 | [diff] [blame] | 467 | |
Andrew M. Kuchling | f5fec3c | 2001-07-19 01:48:08 +0000 | [diff] [blame] | 468 | \begin{seealso} |
| 469 | |
| 470 | \seepep{261}{Support for `wide' Unicode characters}{PEP written by |
| 471 | Paul Prescod. Not yet accepted or fully implemented.} |
| 472 | |
| 473 | \end{seealso} |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 474 | |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 475 | %====================================================================== |
| 476 | \section{PEP 227: Nested Scopes} |
| 477 | |
| 478 | In Python 2.1, statically nested scopes were added as an optional |
| 479 | feature, to be enabled by a \code{from __future__ import |
| 480 | nested_scopes} directive. In 2.2 nested scopes no longer need to be |
| 481 | specially enabled, but are always enabled. The rest of this section |
| 482 | is a copy of the description of nested scopes from my ``What's New in |
| 483 | Python 2.1'' document; if you read it when 2.1 came out, you can skip |
| 484 | the rest of this section. |
| 485 | |
| 486 | The largest change introduced in Python 2.1, and made complete in 2.2, |
| 487 | is to Python's scoping rules. In Python 2.0, at any given time there |
| 488 | are at most three namespaces used to look up variable names: local, |
| 489 | module-level, and the built-in namespace. This often surprised people |
| 490 | because it didn't match their intuitive expectations. For example, a |
| 491 | nested recursive function definition doesn't work: |
| 492 | |
| 493 | \begin{verbatim} |
| 494 | def f(): |
| 495 | ... |
| 496 | def g(value): |
| 497 | ... |
| 498 | return g(value-1) + 1 |
| 499 | ... |
| 500 | \end{verbatim} |
| 501 | |
| 502 | The function \function{g()} will always raise a \exception{NameError} |
| 503 | exception, because the binding of the name \samp{g} isn't in either |
| 504 | its local namespace or in the module-level namespace. This isn't much |
| 505 | of a problem in practice (how often do you recursively define interior |
| 506 | functions like this?), but this also made using the \keyword{lambda} |
| 507 | statement clumsier, and this was a problem in practice. In code which |
| 508 | uses \keyword{lambda} you can often find local variables being copied |
| 509 | by passing them as the default values of arguments. |
| 510 | |
| 511 | \begin{verbatim} |
| 512 | def find(self, name): |
| 513 | "Return list of any entries equal to 'name'" |
| 514 | L = filter(lambda x, name=name: x == name, |
| 515 | self.list_attribute) |
| 516 | return L |
| 517 | \end{verbatim} |
| 518 | |
| 519 | The readability of Python code written in a strongly functional style |
| 520 | suffers greatly as a result. |
| 521 | |
| 522 | The most significant change to Python 2.2 is that static scoping has |
| 523 | been added to the language to fix this problem. As a first effect, |
| 524 | the \code{name=name} default argument is now unnecessary in the above |
| 525 | example. Put simply, when a given variable name is not assigned a |
| 526 | value within a function (by an assignment, or the \keyword{def}, |
| 527 | \keyword{class}, or \keyword{import} statements), references to the |
| 528 | variable will be looked up in the local namespace of the enclosing |
| 529 | scope. A more detailed explanation of the rules, and a dissection of |
| 530 | the implementation, can be found in the PEP. |
| 531 | |
| 532 | This change may cause some compatibility problems for code where the |
| 533 | same variable name is used both at the module level and as a local |
| 534 | variable within a function that contains further function definitions. |
| 535 | This seems rather unlikely though, since such code would have been |
| 536 | pretty confusing to read in the first place. |
| 537 | |
| 538 | One side effect of the change is that the \code{from \var{module} |
| 539 | import *} and \keyword{exec} statements have been made illegal inside |
| 540 | a function scope under certain conditions. The Python reference |
| 541 | manual has said all along that \code{from \var{module} import *} is |
| 542 | only legal at the top level of a module, but the CPython interpreter |
| 543 | has never enforced this before. As part of the implementation of |
| 544 | nested scopes, the compiler which turns Python source into bytecodes |
| 545 | has to generate different code to access variables in a containing |
| 546 | scope. \code{from \var{module} import *} and \keyword{exec} make it |
| 547 | impossible for the compiler to figure this out, because they add names |
| 548 | to the local namespace that are unknowable at compile time. |
| 549 | Therefore, if a function contains function definitions or |
| 550 | \keyword{lambda} expressions with free variables, the compiler will |
| 551 | flag this by raising a \exception{SyntaxError} exception. |
| 552 | |
| 553 | To make the preceding explanation a bit clearer, here's an example: |
| 554 | |
| 555 | \begin{verbatim} |
| 556 | x = 1 |
| 557 | def f(): |
| 558 | # The next line is a syntax error |
| 559 | exec 'x=2' |
| 560 | def g(): |
| 561 | return x |
| 562 | \end{verbatim} |
| 563 | |
| 564 | Line 4 containing the \keyword{exec} statement is a syntax error, |
| 565 | since \keyword{exec} would define a new local variable named \samp{x} |
| 566 | whose value should be accessed by \function{g()}. |
| 567 | |
| 568 | This shouldn't be much of a limitation, since \keyword{exec} is rarely |
| 569 | used in most Python code (and when it is used, it's often a sign of a |
| 570 | poor design anyway). |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 571 | |
| 572 | \begin{seealso} |
| 573 | |
| 574 | \seepep{227}{Statically Nested Scopes}{Written and implemented by |
| 575 | Jeremy Hylton.} |
| 576 | |
| 577 | \end{seealso} |
| 578 | |
Andrew M. Kuchling | a43e703 | 2001-06-27 20:32:12 +0000 | [diff] [blame] | 579 | |
| 580 | %====================================================================== |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 581 | \section{New and Improved Modules} |
| 582 | |
| 583 | \begin{itemize} |
| 584 | |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 585 | \item The \module{xmlrpclib} module was contributed to the standard |
Andrew M. Kuchling | 8c69c91b | 2001-08-07 14:28:58 +0000 | [diff] [blame] | 586 | library by Fredrik Lundh. It provides support for writing XML-RPC |
| 587 | clients; XML-RPC is a simple remote procedure call protocol built on |
| 588 | top of HTTP and XML. For example, the following snippet retrieves a |
| 589 | list of RSS channels from the O'Reilly Network, and then retrieves a |
| 590 | list of the recent headlines for one channel: |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 591 | |
| 592 | \begin{verbatim} |
| 593 | import xmlrpclib |
| 594 | s = xmlrpclib.Server( |
| 595 | 'http://www.oreillynet.com/meerkat/xml-rpc/server.php') |
| 596 | channels = s.meerkat.getChannels() |
| 597 | # channels is a list of dictionaries, like this: |
| 598 | # [{'id': 4, 'title': 'Freshmeat Daily News'} |
| 599 | # {'id': 190, 'title': '32Bits Online'}, |
| 600 | # {'id': 4549, 'title': '3DGamers'}, ... ] |
| 601 | |
| 602 | # Get the items for one channel |
| 603 | items = s.meerkat.getItems( {'channel': 4} ) |
| 604 | |
| 605 | # 'items' is another list of dictionaries, like this: |
| 606 | # [{'link': 'http://freshmeat.net/releases/52719/', |
| 607 | # 'description': 'A utility which converts HTML to XSL FO.', |
| 608 | # 'title': 'html2fo 0.3 (Default)'}, ... ] |
| 609 | \end{verbatim} |
| 610 | |
Fred Drake | 0d00254 | 2001-07-17 13:55:33 +0000 | [diff] [blame] | 611 | See \url{http://www.xmlrpc.com/} for more information about XML-RPC. |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 612 | |
| 613 | \item The \module{socket} module can be compiled to support IPv6; |
Andrew M. Kuchling | ddeb135 | 2001-07-16 14:35:52 +0000 | [diff] [blame] | 614 | specify the \longprogramopt{enable-ipv6} option to Python's configure |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 615 | script. (Contributed by Jun-ichiro ``itojun'' Hagino.) |
| 616 | |
| 617 | \item Two new format characters were added to the \module{struct} |
| 618 | module for 64-bit integers on platforms that support the C |
| 619 | \ctype{long long} type. \samp{q} is for a signed 64-bit integer, |
| 620 | and \samp{Q} is for an unsigned one. The value is returned in |
| 621 | Python's long integer type. (Contributed by Tim Peters.) |
| 622 | |
| 623 | \item In the interpreter's interactive mode, there's a new built-in |
| 624 | function \function{help()}, that uses the \module{pydoc} module |
| 625 | introduced in Python 2.1 to provide interactive. |
| 626 | \code{help(\var{object})} displays any available help text about |
| 627 | \var{object}. \code{help()} with no argument puts you in an online |
| 628 | help utility, where you can enter the names of functions, classes, |
| 629 | or modules to read their help text. |
| 630 | (Contributed by Guido van Rossum, using Ka-Ping Yee's \module{pydoc} module.) |
| 631 | |
| 632 | \item Various bugfixes and performance improvements have been made |
Andrew M. Kuchling | 4cf52a9 | 2001-07-17 12:48:48 +0000 | [diff] [blame] | 633 | to the SRE engine underlying the \module{re} module. For example, |
| 634 | \function{re.sub()} will now use \function{string.replace()} |
| 635 | automatically when the pattern and its replacement are both just |
| 636 | literal strings without regex metacharacters. Another contributed |
| 637 | patch speeds up certain Unicode character ranges by a factor of |
| 638 | two. (SRE is maintained by Fredrik Lundh. The BIGCHARSET patch was |
| 639 | contributed by Martin von L\"owis.) |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 640 | |
Andrew M. Kuchling | a6d2a04 | 2001-07-20 18:34:34 +0000 | [diff] [blame] | 641 | \item The \module{imaplib} module, maintained by Piers Lauder, has |
| 642 | support for several new extensions: the NAMESPACE extension defined |
| 643 | in \rfc{2342}, SORT, GETACL and SETACL. (Contributed by Anthony |
| 644 | Baxter and Michel Pelletier.) |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 645 | |
Fred Drake | 0d00254 | 2001-07-17 13:55:33 +0000 | [diff] [blame] | 646 | \item The \module{rfc822} module's parsing of email addresses is |
Andrew M. Kuchling | 4cf52a9 | 2001-07-17 12:48:48 +0000 | [diff] [blame] | 647 | now compliant with \rfc{2822}, an update to \rfc{822}. The module's |
| 648 | name is \emph{not} going to be changed to \samp{rfc2822}. |
| 649 | (Contributed by Barry Warsaw.) |
Andrew M. Kuchling | 7770767 | 2001-07-31 15:51:16 +0000 | [diff] [blame] | 650 | |
| 651 | \item New constants \constant{ascii_letters}, |
| 652 | \constant{ascii_lowercase}, and \constant{ascii_uppercase} were |
| 653 | added to the \module{string} module. There were several modules in |
| 654 | the standard library that used \constant{string.letters} to mean the |
| 655 | ranges A-Za-z, but that assumption is incorrect when locales are in |
| 656 | use, because \constant{string.letters} varies depending on the set |
| 657 | of legal characters defined by the current locale. The buggy |
| 658 | modules have all been fixed to use \constant{ascii_letters} instead. |
| 659 | (Reported by an unknown person; fixed by Fred L. Drake, Jr.) |
| 660 | |
Andrew M. Kuchling | 8c69c91b | 2001-08-07 14:28:58 +0000 | [diff] [blame] | 661 | \item The \module{mimetypes} module now makes it easier to use |
| 662 | alternative MIME-type databases by the addition of a |
| 663 | \class{MimeTypes} class, which takes a list of filenames to be |
| 664 | parsed. (Contributed by Fred L. Drake, Jr.) |
| 665 | |
Andrew M. Kuchling | 7770767 | 2001-07-31 15:51:16 +0000 | [diff] [blame] | 666 | \end{itemize} |
| 667 | |
| 668 | |
| 669 | %====================================================================== |
| 670 | \section{Interpreter Changes and Fixes} |
| 671 | |
| 672 | Some of the changes only affect people who deal with the Python |
| 673 | interpreter at the C level, writing Python extension modules, |
| 674 | embedding the interpreter, or just hacking on the interpreter itself. |
| 675 | If you only write Python code, none of the changes described here will |
| 676 | affect you very much. |
| 677 | |
| 678 | \begin{itemize} |
| 679 | |
| 680 | \item Profiling and tracing functions can now be implemented in C, |
| 681 | which can operate at much higher speeds than Python-based functions |
| 682 | and should reduce the overhead of enabling profiling and tracing, so |
| 683 | it will be of interest to authors of development environments for |
| 684 | Python. Two new C functions were added to Python's API, |
| 685 | \cfunction{PyEval_SetProfile()} and \cfunction{PyEval_SetTrace()}. |
| 686 | The existing \function{sys.setprofile()} and |
| 687 | \function{sys.settrace()} functions still exist, and have simply |
| 688 | been changed to use the new C-level interface. (Contributed by Fred |
| 689 | L. Drake, Jr.) |
| 690 | |
| 691 | \item Another low-level API, primarily of interest to implementors |
| 692 | of Python debuggers and development tools, was added. |
| 693 | \cfunction{PyInterpreterState_Head()} and |
| 694 | \cfunction{PyInterpreterState_Next()} let a caller walk through all |
| 695 | the existing interpreter objects; |
| 696 | \cfunction{PyInterpreterState_ThreadHead()} and |
| 697 | \cfunction{PyThreadState_Next()} allow looping over all the thread |
| 698 | states for a given interpreter. (Contributed by David Beazley.) |
| 699 | |
| 700 | \item A new \samp{et} format sequence was added to |
| 701 | \cfunction{PyArg_ParseTuple}; \samp{et} takes both a parameter and |
| 702 | an encoding name, and converts the parameter to the given encoding |
| 703 | if the parameter turns out to be a Unicode string, or leaves it |
| 704 | alone if it's an 8-bit string, assuming it to already be in the |
| 705 | desired encoding. This differs from the \samp{es} format character, |
| 706 | which assumes that 8-bit strings are in Python's default ASCII |
| 707 | encoding and converts them to the specified new encoding. |
| 708 | (Contributed by M.-A. Lemburg, and used for the MBCS support on |
| 709 | Windows described in the previous section.) |
| 710 | |
| 711 | \item Two new wrapper functions, \cfunction{PyOS_snprintf()} and |
| 712 | \cfunction{PyOS_vsnprintf()} were added. which provide a cross-platform |
| 713 | implementations for the relatively new snprintf()/vsnprintf() C lib |
| 714 | APIs. In contrast to the standard sprintf() and vsprintf() C lib |
| 715 | APIs, these versions apply bounds checking on the used buffer which |
| 716 | enhances protection against buffer overruns. |
| 717 | (Contributed by M.-A. Lemburg.) |
| 718 | |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 719 | \end{itemize} |
| 720 | |
| 721 | |
| 722 | %====================================================================== |
| 723 | \section{Other Changes and Fixes} |
| 724 | |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 725 | % XXX update the patch and bug figures as we go |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 726 | As usual there were a bunch of other improvements and bugfixes |
| 727 | scattered throughout the source tree. A search through the CVS change |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 728 | logs finds there were 43 patches applied, and 77 bugs fixed; both |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 729 | figures are likely to be underestimates. Some of the more notable |
| 730 | changes are: |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 731 | |
| 732 | \begin{itemize} |
| 733 | |
Andrew M. Kuchling | 2cd712b | 2001-07-16 13:39:08 +0000 | [diff] [blame] | 734 | \item Keyword arguments passed to builtin functions that don't take them |
| 735 | now cause a \exception{TypeError} exception to be raised, with the |
| 736 | message "\var{function} takes no keyword arguments". |
| 737 | |
Fred Drake | 0d00254 | 2001-07-17 13:55:33 +0000 | [diff] [blame] | 738 | \item The code for the Mac OS port for Python, maintained by Jack |
Andrew M. Kuchling | 2cd712b | 2001-07-16 13:39:08 +0000 | [diff] [blame] | 739 | Jansen, is now kept in the main Python CVS tree. |
| 740 | |
| 741 | \item The new license introduced with Python 1.6 wasn't |
| 742 | GPL-compatible. This is fixed by some minor textual changes to the |
| 743 | 2.2 license, so Python can now be embedded inside a GPLed program |
| 744 | again. The license changes were also applied to the Python 2.0.1 |
| 745 | and 2.1.1 releases. |
| 746 | |
Andrew M. Kuchling | f4ccf58 | 2001-07-31 01:11:36 +0000 | [diff] [blame] | 747 | \item When presented with a Unicode filename on Windows, Python will |
| 748 | now convert it to an MBCS encoded string, as used by the Microsoft |
| 749 | file APIs. As MBCS is explicitly used by the file APIs, Python's |
| 750 | choice of ASCII as the default encoding turns out to be an |
| 751 | annoyance. |
| 752 | |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 753 | (Contributed by Mark Hammond with assistance from Marc-Andr\'e |
| 754 | Lemburg.) |
| 755 | |
Andrew M. Kuchling | 2cd712b | 2001-07-16 13:39:08 +0000 | [diff] [blame] | 756 | \item The \file{Tools/scripts/ftpmirror.py} script |
| 757 | now parses a \file{.netrc} file, if you have one. |
Andrew M. Kuchling | 4cf52a9 | 2001-07-17 12:48:48 +0000 | [diff] [blame] | 758 | (Contributed by Mike Romberg.) |
Andrew M. Kuchling | 2cd712b | 2001-07-16 13:39:08 +0000 | [diff] [blame] | 759 | |
Andrew M. Kuchling | 4cf52a9 | 2001-07-17 12:48:48 +0000 | [diff] [blame] | 760 | \item Some features of the object returned by the |
| 761 | \function{xrange()} function are now deprecated, and trigger |
| 762 | warnings when they're accessed; they'll disappear in Python 2.3. |
| 763 | \class{xrange} objects tried to pretend they were full sequence |
| 764 | types by supporting slicing, sequence multiplication, and the |
| 765 | \keyword{in} operator, but these features were rarely used and |
| 766 | therefore buggy. The \method{tolist()} method and the |
| 767 | \member{start}, \member{stop}, and \member{step} attributes are also |
| 768 | being deprecated. At the C level, the fourth argument to the |
| 769 | \cfunction{PyRange_New()} function, \samp{repeat}, has also been |
| 770 | deprecated. |
| 771 | |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 772 | \item There were a bunch of patches to the dictionary |
| 773 | implementation, mostly to fix potential core dumps if a dictionary |
| 774 | contains objects that sneakily changed their hash value, or mutated |
| 775 | the dictionary they were contained in. For a while python-dev fell |
| 776 | into a gentle rhythm of Michael Hudson finding a case that dump |
| 777 | core, Tim Peters fixing it, Michael finding another case, and round |
| 778 | and round it went. |
| 779 | |
Andrew M. Kuchling | 4cf52a9 | 2001-07-17 12:48:48 +0000 | [diff] [blame] | 780 | \item On Windows, Python can now be compiled with Borland C thanks |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 781 | to a number of patches contributed by Stephen Hansen. |
Andrew M. Kuchling | 8c69c91b | 2001-08-07 14:28:58 +0000 | [diff] [blame] | 782 | |
Andrew M. Kuchling | f4ccf58 | 2001-07-31 01:11:36 +0000 | [diff] [blame] | 783 | \item Another Windows enhancement: Wise Solutions generously offered |
| 784 | PythonLabs use of their InstallerMaster 8.1 system. Earlier |
| 785 | PythonLabs Windows installers used Wise 5.0a, which was beginning to |
| 786 | show its age. (Packaged up by Tim Peters.) |
| 787 | |
Andrew M. Kuchling | 8c69c91b | 2001-08-07 14:28:58 +0000 | [diff] [blame] | 788 | \item Files ending in \samp{.pyw} can now be imported on Windows. |
| 789 | \samp{.pyw} is a Windows-only thing, used to indicate that a script |
| 790 | needs to be run using PYTHONW.EXE instead of PYTHON.EXE in order to |
| 791 | prevent a DOS console from popping up to display the output. This |
| 792 | patch makes it possible to import such scripts, in case they're also |
| 793 | usable as modules. (Implemented by David Bolen.) |
| 794 | |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 795 | \item On platforms where Python uses the C \cfunction{dlopen()} function |
| 796 | to load extension modules, it's now possible to set the flags used |
| 797 | by \cfunction{dlopen()} using the \function{sys.getdlopenflags()} and |
| 798 | \function{sys.setdlopenflags()} functions. (Contributed by Bram Stolk.) |
Andrew M. Kuchling | 7770767 | 2001-07-31 15:51:16 +0000 | [diff] [blame] | 799 | |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 800 | \end{itemize} |
| 801 | |
| 802 | |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 803 | %====================================================================== |
| 804 | \section{Acknowledgements} |
| 805 | |
| 806 | The author would like to thank the following people for offering |
Andrew M. Kuchling | 6ea9f0b | 2001-07-17 14:50:31 +0000 | [diff] [blame] | 807 | suggestions and corrections to various drafts of this article: Fred |
Andrew M. Kuchling | 9e9c135 | 2001-08-11 03:06:50 +0000 | [diff] [blame] | 808 | Bremmer, Keith Briggs, Fred L. Drake, Jr., Carel Fellinger, Mark |
| 809 | Hammond, Marc-Andr\'e Lemburg, Tim Peters, Neil Schemenauer, Guido van |
| 810 | Rossum. |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 811 | |
| 812 | \end{document} |