5 \title{What's New in Python
2.2}
8 \authoraddress{\email{akuchlin@mems-exchange.org
}}
10 \maketitle\tableofcontents
12 \section{Introduction
}
14 {\large This
document is a draft, and is subject to change until the
15 final version of Python
2.2 is released. Currently it's up to date
16 for Python
2.2 beta
1. Please send any comments, bug reports, or
17 questions, no matter how minor, to
\email{akuchlin@mems-exchange.org
}.
20 This article explains the new features in Python
2.2.
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
28 This article doesn't attempt to provide a complete specification of
29 the new features, but instead provides a convenient overview. For
30 full details, you should refer to the documentation for Python
2.2,
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
36 % XXX These \citetitle marks should get the python.org URLs for the final
37 % release, just as soon as the docs are published there.
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.
41 The final release of Python
2.2 is planned for December
2001.
45 \seeurl{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.
}
52 %======================================================================
53 \section{PEPs
252 and
253: Type and Class Changes
}
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
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.
74 Python
2.2 fixes this, and in the process adds some exciting new
75 capabilities. A brief summary:
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
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.
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,
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
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.
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.
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
117 section will paint only the broad strokes. See section~
\ref{sect-rellinks
},
118 ``Related Links'', for further sources of information about Python
2.2's new
122 \subsection{Old and New Classes
}
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.
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
}:
146 This means that
\keyword{class
} statements that don't have any base
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
}.)
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.
165 To make the set of types complete, new type objects such as
166 \function{dict
} and
\function{file
} have been added. Here's a
167 more interesting example, adding a
\method{lock()
} method to file
171 class LockableFile(file):
172 def lock (self, operation, length=
0, start=
0, whence=
0):
174 return fcntl.lockf(self.fileno(), operation,
175 length, start, whence)
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
}.
185 \subsection{Descriptors
}
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
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
204 Attribute descriptors are objects that live inside class objects, and
205 have a few attributes of their own:
209 \item \member{__name__
} is the attribute's name.
211 \item \member{__doc__
} is the attribute's docstring.
213 \item \method{__get__(
\var{object
})
} is a method that retrieves the
214 attribute value from
\var{object
}.
216 \item \method{__set__(
\var{object
},
\var{value
})
} sets the attribute
217 on
\var{object
} to
\var{value
}.
219 \item \method{__delete__(
\var{object
},
\var{value
})
} deletes the
\var{value
}
220 attribute of
\var{object
}.
223 For example, when you write
\code{obj.x
}, the steps that Python
224 actually performs are:
227 descriptor = obj.__class__.x
228 descriptor.__get__(obj)
231 For methods,
\method{descriptor.__get__
} returns a temporary object that's
232 callable, and wraps up the instance and the method to be called on it.
233 This is also why static methods and class methods are now possible;
234 they have descriptors that wrap up just the method, or the method and
235 the class. As a brief explanation of these new kinds of methods,
236 static methods aren't passed the instance, and therefore resemble
237 regular functions. Class methods are passed the class of the object,
238 but not the object itself. Static and class methods are defined like
247 def g(cls, arg1, arg2):
252 The
\function{staticmethod()
} function takes the function
253 \function{f
}, and returns it wrapped up in a descriptor so it can be
254 stored in the class object. You might expect there to be special
255 syntax for creating such methods (
\code{def static f()
},
256 \code{defstatic f()
}, or something like that) but no such syntax has
257 been defined yet; that's been left for future versions.
259 More new features, such as slots and properties, are also implemented
260 as new kinds of descriptors, and it's not difficult to write a
261 descriptor class that does something novel. For example, it would be
262 possible to write a descriptor class that made it possible to write
263 Eiffel-style preconditions and postconditions for a method. A class
264 that used this feature might be defined like this:
267 from eiffel import eiffelmethod
270 def f(self, arg1, arg2):
271 # The actual function
273 # Check preconditions
275 # Check postconditions
277 f = eiffelmethod(f, pre_f, post_f)
280 Note that a person using the new
\function{eiffelmethod()
} doesn't
281 have to understand anything about descriptors. This is why I think
282 the new features don't increase the basic complexity of the language.
283 There will be a few wizards who need to know about it in order to
284 write
\function{eiffelmethod()
} or the ZODB or whatever, but most
285 users will just write code on top of the resulting libraries and
286 ignore the implementation details.
288 \subsection{Multiple Inheritance: The Diamond Rule
}
290 Multiple inheritance has also been made more useful through changing
291 the rules under which names are resolved. Consider this set of classes
292 (diagram taken from
\pep{253} by Guido van Rossum):
296 ^ ^ def save(self): ...
302 ^ ^ def save(self): ...
310 The lookup rule for classic classes is simple but not very smart; the
311 base classes are searched depth-first, going from left to right. A
312 reference to
\method{D.save
} will search the classes
\class{D
},
313 \class{B
}, and then
\class{A
}, where
\method{save()
} would be found
314 and returned.
\method{C.save()
} would never be found at all. This is
315 bad, because if
\class{C
}'s
\method{save()
} method is saving some
316 internal state specific to
\class{C
}, not calling it will result in
317 that state never getting saved.
319 New-style classes follow a different algorithm that's a bit more
320 complicated to explain, but does the right thing in this situation.
324 \item List all the base classes, following the classic lookup rule and
325 include a class multiple times if it's visited repeatedly. In the
326 above example, the list of visited classes is
[\class{D
},
\class{B
},
327 \class{A
},
\class{C
},
\class{A
}].
329 \item Scan the list for duplicated classes. If any are found, remove
330 all but one occurrence, leaving the
\emph{last
} one in the list. In
331 the above example, the list becomes
[\class{D
},
\class{B
},
\class{C
},
332 \class{A
}] after dropping duplicates.
336 Following this rule, referring to
\method{D.save()
} will return
337 \method{C.save()
}, which is the behaviour we're after. This lookup
338 rule is the same as the one followed by Common Lisp.
340 % XXX mention super()
343 \subsection{Attribute Access
}
345 A fair number of sophisticated Python classes define hooks for
346 attribute access using
\method{__getattr__
}; most commonly this is
347 done for convenience, to make code more readable by automatically
348 mapping an attribute access such as
\code{obj.parent
} into a method
349 call such as
\code{obj.get_parent()
}. Python
2.2 adds some new ways
350 of controlling attribute access.
352 First,
\method{__getattr__(
\var{attr_name
})
} is still supported by
353 new-style classes, and nothing about it has changed. As before, it
354 will be called when an attempt is made to access
\code{obj.foo
} and no
355 attribute named
\samp{foo
} is found in the instance's dictionary.
357 New-style classes also support a new method,
358 \method{__getattribute__(
\var{attr_name
})
}. The difference between
359 the two methods is that
\method{__getattribute__
} is
\emph{always
}
360 called whenever any attribute is accessed, while the old
361 \method{__getattr__
} is only called if
\samp{foo
} isn't found in the
362 instance's dictionary.
364 However, Python
2.2's support for
\dfn{properties
} will often be a
365 simpler way to trap attribute references. Writing a
366 \method{__getattr__
} method is complicated because to avoid recursion
367 you can't use regular attribute accesses inside them, and instead have
368 to mess around with the contents of
\member{__dict__
}.
369 \method{__getattr__
} methods also end up being called by Python when
370 it checks for other methods such as
\method{__repr__
} or
371 \method{__coerce__
}, and so have to be written with this in mind.
372 Finally, calling a function on every attribute access results in a
373 sizable performance loss.
375 \class{property
} is a new built-in type that packages up three
376 functions that get, set, or delete an attribute, and a docstring. For
377 example, if you want to define a
\member{size
} attribute that's
378 computed, but also settable, you could write:
383 result = ... computation ...
385 def set_size (self, size):
386 ... compute something based on the size
387 and set internal state appropriately ...
389 # Define a property. The 'delete this attribute'
390 # method is defined as None, so the attribute
392 size = property(get_size, set_size,
394 "Storage size of this instance")
397 That is certainly clearer and easier to write than a pair of
398 \method{__getattr__
}/
\method{__setattr__
} methods that check for the
399 \member{size
} attribute and handle it specially while retrieving all
400 other attributes from the instance's
\member{__dict__
}. Accesses to
401 \member{size
} are also the only ones which have to perform the work of
402 calling a function, so references to other attributes run at
405 Finally, it's possible to constrain the list of attributes that can be
406 referenced on an object using the new
\member{__slots__
} class attribute.
407 Python objects are usually very dynamic; at any time it's possible to
408 define a new attribute on an instance by just doing
409 \code{obj.new_attr=
1}. This is flexible and convenient, but this
410 flexibility can also lead to bugs, as when you meant to write
411 \code{obj.template = 'a'
} but made a typo and wrote
412 \code{obj.templtae
} by accident.
414 A new-style class can define a class attribute named
\member{__slots__
}
415 to constrain the list of legal attribute names. An example will make
420 ... __slots__ = ('template', 'name')
423 >>> print obj.template
425 >>> obj.template = 'Test'
426 >>> print obj.template
428 >>> obj.templtae = None
429 Traceback (most recent call last):
430 File "<stdin>", line
1, in ?
431 AttributeError: 'C' object has no attribute 'templtae'
434 Note how you get an
\exception{AttributeError
} on the attempt to
435 assign to an attribute not listed in
\member{__slots__
}.
438 \subsection{Related Links
}
439 \label{sect-rellinks
}
441 This section has just been a quick overview of the new features,
442 giving enough of an explanation to start you programming, but many
443 details have been simplified or ignored. Where should you go to get a
444 more complete picture?
446 \url{http://www.python.org/
2.2/descrintro.html
} is a lengthy tutorial
447 introduction to the descriptor features, written by Guido van Rossum.
448 If my description has whetted your appetite, go read this tutorial
449 next, because it goes into much more detail about the new features
450 while still remaining quite easy to read.
452 Next, there are two relevant PEPs,
\pep{252} and
\pep{253}.
\pep{252}
453 is titled "Making Types Look More Like Classes", and covers the
454 descriptor API.
\pep{253} is titled "Subtyping Built-in Types", and
455 describes the changes to type objects that make it possible to subtype
456 built-in objects.
\pep{253} is the more complicated PEP of the two,
457 and at a few points the necessary explanations of types and meta-types
458 may cause your head to explode. Both PEPs were written and
459 implemented by Guido van Rossum, with substantial assistance from the
460 rest of the Zope Corp. team.
462 Finally, there's the ultimate authority: the source code. Most of the
463 machinery for the type handling is in
\file{Objects/typeobject.c
}, but
464 you should only resort to it after all other avenues have been
465 exhausted, including posting a question to python-list or python-dev.
468 %======================================================================
469 \section{PEP
234: Iterators
}
471 Another significant addition to
2.2 is an iteration interface at both
472 the C and Python levels. Objects can define how they can be looped
475 In Python versions up to
2.1, the usual way to make
\code{for item in
476 obj
} work is to define a
\method{__getitem__()
} method that looks
480 def __getitem__(self, index):
484 \method{__getitem__()
} is more properly used to define an indexing
485 operation on an object so that you can write
\code{obj
[5]} to retrieve
486 the sixth element. It's a bit misleading when you're using this only
487 to support
\keyword{for
} loops. Consider some file-like object that
488 wants to be looped over; the
\var{index
} parameter is essentially
489 meaningless, as the class probably assumes that a series of
490 \method{__getitem__()
} calls will be made with
\var{index
}
491 incrementing by one each time. In other words, the presence of the
492 \method{__getitem__()
} method doesn't mean that using
\code{file
[5]}
493 to randomly access the sixth element will work, though it really should.
495 In Python
2.2, iteration can be implemented separately, and
496 \method{__getitem__()
} methods can be limited to classes that really
497 do support random access. The basic idea of iterators is
498 simple. A new built-in function,
\function{iter(obj)
} or
499 \code{iter(
\var{C
},
\var{sentinel
})
}, is used to get an iterator.
500 \function{iter(obj)
} returns an iterator for the object
\var{obj
},
501 while
\code{iter(
\var{C
},
\var{sentinel
})
} returns an iterator that
502 will invoke the callable object
\var{C
} until it returns
503 \var{sentinel
} to signal that the iterator is done.
505 Python classes can define an
\method{__iter__()
} method, which should
506 create and return a new iterator for the object; if the object is its
507 own iterator, this method can just return
\code{self
}. In particular,
508 iterators will usually be their own iterators. Extension types
509 implemented in C can implement a
\code{tp_iter
} function in order to
510 return an iterator, and extension types that want to behave as
511 iterators can define a
\code{tp_iternext
} function.
513 So, after all this, what do iterators actually do? They have one
514 required method,
\method{next()
}, which takes no arguments and returns
515 the next value. When there are no more values to be returned, calling
516 \method{next()
} should raise the
\exception{StopIteration
} exception.
522 <iterator object at
0x8116870>
530 Traceback (most recent call last):
531 File "<stdin>", line
1, in ?
536 In
2.2, Python's
\keyword{for
} statement no longer expects a sequence;
537 it expects something for which
\function{iter()
} will return an iterator.
538 For backward compatibility and convenience, an iterator is
539 automatically constructed for sequences that don't implement
540 \method{__iter__()
} or a
\code{tp_iter
} slot, so
\code{for i in
541 [1,
2,
3]} will still work. Wherever the Python interpreter loops over
542 a sequence, it's been changed to use the iterator protocol. This
543 means you can do things like this:
553 Iterator support has been added to some of Python's basic types.
554 Calling
\function{iter()
} on a dictionary will return an iterator
555 which loops over its keys:
558 >>> m =
{'Jan':
1, 'Feb':
2, 'Mar':
3, 'Apr':
4, 'May':
5, 'Jun':
6,
559 ... 'Jul':
7, 'Aug':
8, 'Sep':
9, 'Oct':
10, 'Nov':
11, 'Dec':
12}
560 >>> for key in m: print key, m
[key
]
576 That's just the default behaviour. If you want to iterate over keys,
577 values, or key/value pairs, you can explicitly call the
578 \method{iterkeys()
},
\method{itervalues()
}, or
\method{iteritems()
}
579 methods to get an appropriate iterator. In a minor related change,
580 the
\keyword{in
} operator now works on dictionaries, so
581 \code{\var{key
} in dict
} is now equivalent to
582 \code{dict.has_key(
\var{key
})
}.
584 Files also provide an iterator, which calls the
\method{readline()
}
585 method until there are no more lines in the file. This means you can
586 now read each line of a file using code like this:
590 # do something for each line
594 Note that you can only go forward in an iterator; there's no way to
595 get the previous element, reset the iterator, or make a copy of it.
596 An iterator object could provide such additional capabilities, but the
597 iterator protocol only requires a
\method{next()
} method.
601 \seepep{234}{Iterators
}{Written by Ka-Ping Yee and GvR; implemented
602 by the Python Labs crew, mostly by GvR and Tim Peters.
}
607 %======================================================================
608 \section{PEP
255: Simple Generators
}
610 Generators are another new feature, one that interacts with the
611 introduction of iterators.
613 You're doubtless familiar with how function calls work in Python or
614 C. When you call a function, it gets a private namespace where its local
615 variables are created. When the function reaches a
\keyword{return
}
616 statement, the local variables are destroyed and the resulting value
617 is returned to the caller. A later call to the same function will get
618 a fresh new set of local variables. But, what if the local variables
619 weren't thrown away on exiting a function? What if you could later
620 resume the function where it left off? This is what generators
621 provide; they can be thought of as resumable functions.
623 Here's the simplest example of a generator function:
626 def generate_ints(N):
631 A new keyword,
\keyword{yield
}, was introduced for generators. Any
632 function containing a
\keyword{yield
} statement is a generator
633 function; this is detected by Python's bytecode compiler which
634 compiles the function specially as a result. Because a new keyword was
635 introduced, generators must be explicitly enabled in a module by
636 including a
\code{from __future__ import generators
} statement near
637 the top of the module's source code. In Python
2.3 this statement
638 will become unnecessary.
640 When you call a generator function, it doesn't return a single value;
641 instead it returns a generator object that supports the iterator
642 protocol. On executing the
\keyword{yield
} statement, the generator
643 outputs the value of
\code{i
}, similar to a
\keyword{return
}
644 statement. The big difference between
\keyword{yield
} and a
645 \keyword{return
} statement is that on reaching a
\keyword{yield
} the
646 generator's state of execution is suspended and local variables are
647 preserved. On the next call to the generator's
\code{.next()
} method,
648 the function will resume executing immediately after the
649 \keyword{yield
} statement. (For complicated reasons, the
650 \keyword{yield
} statement isn't allowed inside the
\keyword{try
} block
651 of a
\code{try...finally
} statement; read
\pep{255} for a full
652 explanation of the interaction between
\keyword{yield
} and
655 Here's a sample usage of the
\function{generate_ints
} generator:
658 >>> gen = generate_ints(
3)
660 <generator object at
0x8117f90>
668 Traceback (most recent call last):
669 File "<stdin>", line
1, in ?
670 File "<stdin>", line
2, in generate_ints
674 You could equally write
\code{for i in generate_ints(
5)
}, or
675 \code{a,b,c = generate_ints(
3)
}.
677 Inside a generator function, the
\keyword{return
} statement can only
678 be used without a value, and signals the end of the procession of
679 values; afterwards the generator cannot return any further values.
680 \keyword{return
} with a value, such as
\code{return
5}, is a syntax
681 error inside a generator function. The end of the generator's results
682 can also be indicated by raising
\exception{StopIteration
} manually,
683 or by just letting the flow of execution fall off the bottom of the
686 You could achieve the effect of generators manually by writing your
687 own class and storing all the local variables of the generator as
688 instance variables. For example, returning a list of integers could
689 be done by setting
\code{self.count
} to
0, and having the
690 \method{next()
} method increment
\code{self.count
} and return it.
691 However, for a moderately complicated generator, writing a
692 corresponding class would be much messier.
693 \file{Lib/test/test_generators.py
} contains a number of more
694 interesting examples. The simplest one implements an in-order
695 traversal of a tree using generators recursively.
698 # A recursive generator that generates Tree leaves in in-order.
701 for x in inorder(t.left):
704 for x in inorder(t.right):
708 Two other examples in
\file{Lib/test/test_generators.py
} produce
709 solutions for the N-Queens problem (placing $N$ queens on an $NxN$
710 chess board so that no queen threatens another) and the Knight's Tour
711 (a route that takes a knight to every square of an $NxN$ chessboard
712 without visiting any square twice).
714 The idea of generators comes from other programming languages,
715 especially Icon (
\url{http://www.cs.arizona.edu/icon/
}), where the
716 idea of generators is central. In Icon, every
717 expression and function call behaves like a generator. One example
718 from ``An Overview of the Icon Programming Language'' at
719 \url{http://www.cs.arizona.edu/icon/docs/ipd266.htm
} gives an idea of
720 what this looks like:
723 sentence := "Store it in the neighboring harbor"
724 if (i := find("or", sentence)) >
5 then write(i)
727 In Icon the
\function{find()
} function returns the indexes at which the
728 substring ``or'' is found:
3,
23,
33. In the
\keyword{if
} statement,
729 \code{i
} is first assigned a value of
3, but
3 is less than
5, so the
730 comparison fails, and Icon retries it with the second value of
23.
23
731 is greater than
5, so the comparison now succeeds, and the code prints
732 the value
23 to the screen.
734 Python doesn't go nearly as far as Icon in adopting generators as a
735 central concept. Generators are considered a new part of the core
736 Python language, but learning or using them isn't compulsory; if they
737 don't solve any problems that you have, feel free to ignore them.
738 One novel feature of Python's interface as compared to
739 Icon's is that a generator's state is represented as a concrete object
740 (the iterator) that can be passed around to other functions or stored
745 \seepep{255}{Simple Generators
}{Written by Neil Schemenauer, Tim
746 Peters, Magnus Lie Hetland. Implemented mostly by Neil Schemenauer
747 and Tim Peters, with other fixes from the Python Labs crew.
}
752 %======================================================================
753 \section{PEP
237: Unifying Long Integers and Integers
}
755 In recent versions, the distinction between regular integers, which
756 are
32-bit values on most machines, and long integers, which can be of
757 arbitrary size, was becoming an annoyance. For example, on platforms
758 that support files larger than
\code{2**
32} bytes, the
759 \method{tell()
} method of file objects has to return a long integer.
760 However, there were various bits of Python that expected plain
761 integers and would raise an error if a long integer was provided
762 instead. For example, in Python
1.5, only regular integers
763 could be used as a slice index, and
\code{'abc'
[1L:
]} would raise a
764 \exception{TypeError
} exception with the message 'slice index must be
767 Python
2.2 will shift values from short to long integers as required.
768 The 'L' suffix is no longer needed to indicate a long integer literal,
769 as now the compiler will choose the appropriate type. (Using the 'L'
770 suffix will be discouraged in future
2.x versions of Python,
771 triggering a warning in Python
2.4, and probably dropped in Python
772 3.0.) Many operations that used to raise an
\exception{OverflowError
}
773 will now return a long integer as their result. For example:
779 18446744073709551616L
782 In most cases, integers and long integers will now be treated
783 identically. You can still distinguish them with the
784 \function{type()
} built-in function, but that's rarely needed.
788 \seepep{237}{Unifying Long Integers and Integers
}{Written by
789 Moshe Zadka and Guido van Rossum. Implemented mostly by Guido van
795 %======================================================================
796 \section{PEP
238: Changing the Division Operator
}
798 The most controversial change in Python
2.2 heralds the start of an effort
799 to fix an old design flaw that's been in Python from the beginning.
800 Currently Python's division operator,
\code{/
}, behaves like C's
801 division operator when presented with two integer arguments: it
802 returns an integer result that's truncated down when there would be
803 a fractional part. For example,
\code{3/
2} is
1, not
1.5, and
804 \code{(-
1)/
2} is -
1, not -
0.5. This means that the results of divison
805 can vary unexpectedly depending on the type of the two operands and
806 because Python is dynamically typed, it can be difficult to determine
807 the possible types of the operands.
809 (The controversy is over whether this is
\emph{really
} a design flaw,
810 and whether it's worth breaking existing code to fix this. It's
811 caused endless discussions on python-dev, and in July
2001 erupted into an
812 storm of acidly sarcastic postings on
\newsgroup{comp.lang.python
}. I
813 won't argue for either side here and will stick to describing what's
814 implemented in
2.2. Read
\pep{238} for a summary of arguments and
817 Because this change might break code, it's being introduced very
818 gradually. Python
2.2 begins the transition, but the switch won't be
819 complete until Python
3.0.
821 First, I'll borrow some terminology from
\pep{238}. ``True division'' is the
822 division that most non-programmers are familiar with:
3/
2 is
1.5,
1/
4
823 is
0.25, and so forth. ``Floor division'' is what Python's
\code{/
}
824 operator currently does when given integer operands; the result is the
825 floor of the value returned by true division. ``Classic division'' is
826 the current mixed behaviour of
\code{/
}; it returns the result of
827 floor division when the operands are integers, and returns the result
828 of true division when one of the operands is a floating-point number.
830 Here are the changes
2.2 introduces:
834 \item A new operator,
\code{//
}, is the floor division operator.
835 (Yes, we know it looks like
\Cpp's comment symbol.)
\code{//
}
836 \emph{always
} performs floor division no matter what the types of
837 its operands are, so
\code{1 //
2} is
0 and
\code{1.0 //
2.0} is also
840 \code{//
} is always available in Python
2.2; you don't need to enable
841 it using a
\code{__future__
} statement.
843 \item By including a
\code{from __future__ import division
} in a
844 module, the
\code{/
} operator will be changed to return the result of
845 true division, so
\code{1/
2} is
0.5. Without the
\code{__future__
}
846 statement,
\code{/
} still means classic division. The default meaning
847 of
\code{/
} will not change until Python
3.0.
849 \item Classes can define methods called
\method{__truediv__
} and
850 \method{__floordiv__
} to overload the two division operators. At the
851 C level, there are also slots in the
\code{PyNumberMethods
} structure
852 so extension types can define the two operators.
854 \item Python
2.2 supports some command-line arguments for testing
855 whether code will works with the changed division semantics. Running
856 python with
\programopt{-Q warn
} will cause a warning to be issued
857 whenever division is applied to two integers. You can use this to
858 find code that's affected by the change and fix it. By default,
859 Python
2.2 will simply perform classic division without a warning; the
860 warning will be turned on by default in Python
2.3.
866 \seepep{238}{Changing the Division Operator
}{Written by Moshe Zadka and
867 Guido van Rossum. Implemented by Guido van Rossum..
}
872 %======================================================================
873 \section{Unicode Changes
}
875 Python's Unicode support has been enhanced a bit in
2.2. Unicode
876 strings are usually stored as UCS-
2, as
16-bit unsigned integers.
877 Python
2.2 can also be compiled to use UCS-
4,
32-bit unsigned
878 integers, as its internal encoding by supplying
879 \longprogramopt{enable-unicode=ucs4
} to the configure script.
880 (It's also possible to specify
881 \longprogramopt{disable-unicode
} to completely disable Unicode
884 When built to use UCS-
4 (a ``wide Python''), the interpreter can
885 natively handle Unicode characters from U+
000000 to U+
110000, so the
886 range of legal values for the
\function{unichr()
} function is expanded
887 accordingly. Using an interpreter compiled to use UCS-
2 (a ``narrow
888 Python''), values greater than
65535 will still cause
889 \function{unichr()
} to raise a
\exception{ValueError
} exception.
890 This is all described in
\pep{261}, ``Support for `wide' Unicode
891 characters''; consult it for further details.
893 Another change is simpler to explain. Since their introduction,
894 Unicode strings have supported an
\method{encode()
} method to convert
895 the string to a selected encoding such as UTF-
8 or Latin-
1. A
896 symmetric
\method{decode(
\optional{\var{encoding
}})
} method has been
897 added to
8-bit strings (though not to Unicode strings) in
2.2.
898 \method{decode()
} assumes that the string is in the specified encoding
899 and decodes it, returning whatever is returned by the codec.
901 Using this new feature, codecs have been added for tasks not directly
902 related to Unicode. For example, codecs have been added for
903 uu-encoding, MIME's base64 encoding, and compression with the
904 \module{zlib
} module:
907 >>> s = """Here is a lengthy piece of redundant, overly verbose,
908 ... and repetitive text.
910 >>> data = s.encode('zlib')
912 'x
\x9c\r\xc9\xc1\r\x80 \x10\x04\xc0?Ul...'
913 >>> data.decode('zlib')
914 'Here is a lengthy piece of redundant, overly verbose,
\nand repetitive text.
\n'
915 >>> print s.encode('uu')
917 M2&
5R92!I<R!A(&QE;F=T:'D@<&EE8V4@;V8@<F5D=
6YD86YT+"!O=F5R;'D@
918 >=F5R8F
]S92P*
86YD(')E<&
5T:
71I=F4@=&
5X="X*
921 >>> "sheesh".encode('rot-
13')
925 To convert a class instance to Unicode, a
\method{__unicode__
} method
926 can be defined by a class, analogous to
\method{__str__
}.
928 \method{encode()
},
\method{decode()
}, and
\method{__unicode__
} were
929 implemented by Marc-Andr\'e Lemburg. The changes to support using
930 UCS-
4 internally were implemented by Fredrik Lundh and Martin von
935 \seepep{261}{Support for `wide' Unicode characters
}{Written by
941 %======================================================================
942 \section{PEP
227: Nested Scopes
}
944 In Python
2.1, statically nested scopes were added as an optional
945 feature, to be enabled by a
\code{from __future__ import
946 nested_scopes
} directive. In
2.2 nested scopes no longer need to be
947 specially enabled, and are now always present. The rest of this section
948 is a copy of the description of nested scopes from my ``What's New in
949 Python
2.1''
document; if you read it when
2.1 came out, you can skip
950 the rest of this section.
952 The largest change introduced in Python
2.1, and made complete in
2.2,
953 is to Python's scoping rules. In Python
2.0, at any given time there
954 are at most three namespaces used to look up variable names: local,
955 module-level, and the built-in namespace. This often surprised people
956 because it didn't match their intuitive expectations. For example, a
957 nested recursive function definition doesn't work:
964 return g(value-
1) +
1
968 The function
\function{g()
} will always raise a
\exception{NameError
}
969 exception, because the binding of the name
\samp{g
} isn't in either
970 its local namespace or in the module-level namespace. This isn't much
971 of a problem in practice (how often do you recursively define interior
972 functions like this?), but this also made using the
\keyword{lambda
}
973 statement clumsier, and this was a problem in practice. In code which
974 uses
\keyword{lambda
} you can often find local variables being copied
975 by passing them as the default values of arguments.
978 def find(self, name):
979 "Return list of any entries equal to 'name'"
980 L = filter(lambda x, name=name: x == name,
985 The readability of Python code written in a strongly functional style
986 suffers greatly as a result.
988 The most significant change to Python
2.2 is that static scoping has
989 been added to the language to fix this problem. As a first effect,
990 the
\code{name=name
} default argument is now unnecessary in the above
991 example. Put simply, when a given variable name is not assigned a
992 value within a function (by an assignment, or the
\keyword{def
},
993 \keyword{class
}, or
\keyword{import
} statements), references to the
994 variable will be looked up in the local namespace of the enclosing
995 scope. A more detailed explanation of the rules, and a dissection of
996 the implementation, can be found in the PEP.
998 This change may cause some compatibility problems for code where the
999 same variable name is used both at the module level and as a local
1000 variable within a function that contains further function definitions.
1001 This seems rather unlikely though, since such code would have been
1002 pretty confusing to read in the first place.
1004 One side effect of the change is that the
\code{from
\var{module
}
1005 import *
} and
\keyword{exec
} statements have been made illegal inside
1006 a function scope under certain conditions. The Python reference
1007 manual has said all along that
\code{from
\var{module
} import *
} is
1008 only legal at the top level of a module, but the CPython interpreter
1009 has never enforced this before. As part of the implementation of
1010 nested scopes, the compiler which turns Python source into bytecodes
1011 has to generate different code to access variables in a containing
1012 scope.
\code{from
\var{module
} import *
} and
\keyword{exec
} make it
1013 impossible for the compiler to figure this out, because they add names
1014 to the local namespace that are unknowable at compile time.
1015 Therefore, if a function contains function definitions or
1016 \keyword{lambda
} expressions with free variables, the compiler will
1017 flag this by raising a
\exception{SyntaxError
} exception.
1019 To make the preceding explanation a bit clearer, here's an example:
1024 # The next line is a syntax error
1030 Line
4 containing the
\keyword{exec
} statement is a syntax error,
1031 since
\keyword{exec
} would define a new local variable named
\samp{x
}
1032 whose value should be accessed by
\function{g()
}.
1034 This shouldn't be much of a limitation, since
\keyword{exec
} is rarely
1035 used in most Python code (and when it is used, it's often a sign of a
1036 poor design anyway).
1040 \seepep{227}{Statically Nested Scopes
}{Written and implemented by
1046 %======================================================================
1047 \section{New and Improved Modules
}
1051 \item The
\module{xmlrpclib
} module was contributed to the standard
1052 library by Fredrik Lundh, provding support for writing XML-RPC
1053 clients. XML-RPC is a simple remote procedure call protocol built on
1054 top of HTTP and XML. For example, the following snippet retrieves a
1055 list of RSS channels from the O'Reilly Network, and then
1056 lists the recent headlines for one channel:
1060 s = xmlrpclib.Server(
1061 'http://www.oreillynet.com/meerkat/xml-rpc/server.php')
1062 channels = s.meerkat.getChannels()
1063 # channels is a list of dictionaries, like this:
1064 #
[{'id':
4, 'title': 'Freshmeat Daily News'
}
1065 #
{'id':
190, 'title': '
32Bits Online'
},
1066 #
{'id':
4549, 'title': '
3DGamers'
}, ...
]
1068 # Get the items for one channel
1069 items = s.meerkat.getItems(
{'channel':
4} )
1071 # 'items' is another list of dictionaries, like this:
1072 #
[{'link': 'http://freshmeat.net/releases/
52719/',
1073 # 'description': 'A utility which converts HTML to XSL FO.',
1074 # 'title': 'html2fo
0.3 (Default)'
}, ...
]
1077 The
\module{SimpleXMLRPCServer
} module makes it easy to create
1078 straightforward XML-RPC servers. See
\url{http://www.xmlrpc.com/
} for
1079 more information about XML-RPC.
1081 \item The new
\module{hmac
} module implements the HMAC
1082 algorithm described by
\rfc{2104}.
1083 (Contributed by Gerhard H\"aring.)
1085 \item Several functions that originally returned lengthy tuples now
1086 return pseudo-sequences that still behave like tuples but also have
1087 mnemonic attributes such as member
{st_mtime
} or
\member{tm_year
}.
1088 The enhanced functions include
\function{stat()
},
1089 \function{fstat()
},
\function{statvfs()
}, and
\function{fstatvfs()
}
1090 in the
\module{os
} module, and
\function{localtime()
},
1091 \function{gmtime()
}, and
\function{strptime()
} in the
\module{time
}
1094 For example, to obtain a file's size using the old tuples, you'd end
1095 up writing something like
\code{file_size =
1096 os.stat(filename)
[stat.ST_SIZE
]}, but now this can be written more
1097 clearly as
\code{file_size = os.stat(filename).st_size
}.
1099 The original patch for this feature was contributed by Nick Mathewson.
1101 \item The Python profiler has been extensively reworked and various
1102 errors in its output have been corrected. (Contributed by Fred
1103 Fred~L. Drake, Jr. and Tim Peters.)
1105 \item The
\module{socket
} module can be compiled to support IPv6;
1106 specify the
\longprogramopt{enable-ipv6
} option to Python's configure
1107 script. (Contributed by Jun-ichiro ``itojun'' Hagino.)
1109 \item Two new format characters were added to the
\module{struct
}
1110 module for
64-bit integers on platforms that support the C
1111 \ctype{long long
} type.
\samp{q
} is for a signed
64-bit integer,
1112 and
\samp{Q
} is for an unsigned one. The value is returned in
1113 Python's long integer type. (Contributed by Tim Peters.)
1115 \item In the interpreter's interactive mode, there's a new built-in
1116 function
\function{help()
} that uses the
\module{pydoc
} module
1117 introduced in Python
2.1 to provide interactive help.
1118 \code{help(
\var{object
})
} displays any available help text about
1119 \var{object
}.
\code{help()
} with no argument puts you in an online
1120 help utility, where you can enter the names of functions, classes,
1121 or modules to read their help text.
1122 (Contributed by Guido van Rossum, using Ka-Ping Yee's
\module{pydoc
} module.)
1124 \item Various bugfixes and performance improvements have been made
1125 to the SRE engine underlying the
\module{re
} module. For example,
1126 the
\function{re.sub()
} and
\function{re.split()
} functions have
1127 been rewritten in C. Another contributed patch speeds up certain
1128 Unicode character ranges by a factor of two, and a new
\method{finditer()
}
1129 method that returns an iterator over all the non-overlapping matches in
1131 (SRE is maintained by
1132 Fredrik Lundh. The BIGCHARSET patch was contributed by Martin von
1135 \item The
\module{smtplib
} module now supports
\rfc{2487}, ``Secure
1136 SMTP over TLS'', so it's now possible to encrypt the SMTP traffic
1137 between a Python program and the mail transport agent being handed a
1138 message.
\module{smtplib
} also supports SMTP authentication.
1139 (Contributed by Gerhard H\"aring.)
1141 \item The
\module{imaplib
} module, maintained by Piers Lauder, has
1142 support for several new extensions: the NAMESPACE extension defined
1143 in
\rfc{2342}, SORT, GETACL and SETACL. (Contributed by Anthony
1144 Baxter and Michel Pelletier.)
1146 % XXX should the 'email' module get a section of its own?
1147 \item The
\module{rfc822
} module's parsing of email addresses is now
1148 compliant with
\rfc{2822}, an update to
\rfc{822}. (The module's
1149 name is
\emph{not
} going to be changed to
\samp{rfc2822
}.) A new
1150 package,
\module{email
}, has also been added for parsing and
1151 generating e-mail messages. (Contributed by Barry Warsaw, and
1152 arising out of his work on Mailman.)
1154 \item The
\module{difflib
} module now contains a new
\class{Differ
}
1155 class for producing human-readable lists of changes (a ``delta'')
1156 between two sequences of lines of text. There are also two
1157 generator functions,
\function{ndiff()
} and
\function{restore()
},
1158 which respectively return a delta from two sequences, or one of the
1159 original sequences from a delta. (Grunt work contributed by David
1160 Goodger, from ndiff.py code by Tim Peters who then did the
1163 \item New constants
\constant{ascii_letters
},
1164 \constant{ascii_lowercase
}, and
\constant{ascii_uppercase
} were
1165 added to the
\module{string
} module. There were several modules in
1166 the standard library that used
\constant{string.letters
} to mean the
1167 ranges A-Za-z, but that assumption is incorrect when locales are in
1168 use, because
\constant{string.letters
} varies depending on the set
1169 of legal characters defined by the current locale. The buggy
1170 modules have all been fixed to use
\constant{ascii_letters
} instead.
1171 (Reported by an unknown person; fixed by Fred~L. Drake, Jr.)
1173 \item The
\module{mimetypes
} module now makes it easier to use
1174 alternative MIME-type databases by the addition of a
1175 \class{MimeTypes
} class, which takes a list of filenames to be
1176 parsed. (Contributed by Fred~L. Drake, Jr.)
1178 \item A
\class{Timer
} class was added to the
\module{threading
}
1179 module that allows scheduling an activity to happen at some future
1180 time. (Contributed by Itamar Shtull-Trauring.)
1185 %======================================================================
1186 \section{Interpreter Changes and Fixes
}
1188 Some of the changes only affect people who deal with the Python
1189 interpreter at the C level because they're writing Python extension modules,
1190 embedding the interpreter, or just hacking on the interpreter itself.
1191 If you only write Python code, none of the changes described here will
1192 affect you very much.
1196 \item Profiling and tracing functions can now be implemented in C,
1197 which can operate at much higher speeds than Python-based functions
1198 and should reduce the overhead of profiling and tracing. This
1199 will be of interest to authors of development environments for
1200 Python. Two new C functions were added to Python's API,
1201 \cfunction{PyEval_SetProfile()
} and
\cfunction{PyEval_SetTrace()
}.
1202 The existing
\function{sys.setprofile()
} and
1203 \function{sys.settrace()
} functions still exist, and have simply
1204 been changed to use the new C-level interface. (Contributed by Fred
1207 \item Another low-level API, primarily of interest to implementors
1208 of Python debuggers and development tools, was added.
1209 \cfunction{PyInterpreterState_Head()
} and
1210 \cfunction{PyInterpreterState_Next()
} let a caller walk through all
1211 the existing interpreter objects;
1212 \cfunction{PyInterpreterState_ThreadHead()
} and
1213 \cfunction{PyThreadState_Next()
} allow looping over all the thread
1214 states for a given interpreter. (Contributed by David Beazley.)
1216 \item A new
\samp{et
} format sequence was added to
1217 \cfunction{PyArg_ParseTuple
};
\samp{et
} takes both a parameter and
1218 an encoding name, and converts the parameter to the given encoding
1219 if the parameter turns out to be a Unicode string, or leaves it
1220 alone if it's an
8-bit string, assuming it to already be in the
1221 desired encoding. This differs from the
\samp{es
} format character,
1222 which assumes that
8-bit strings are in Python's default ASCII
1223 encoding and converts them to the specified new encoding.
1224 (Contributed by M.-A. Lemburg, and used for the MBCS support on
1225 Windows described in the following section.)
1227 \item A different argument parsing function,
1228 \cfunction{PyArg_UnpackTuple()
}, has been added that's simpler and
1229 presumably faster. Instead of specifying a format string, the
1230 caller simply gives the minimum and maximum number of arguments
1231 expected, and a set of pointers to
\code{PyObject*
} variables that
1232 will be filled in with argument values.
1234 \item Two new flags
\constant{METH_NOARGS
} and
\constant{METH_O
} are
1235 available in method definition tables to simplify implementation of
1236 methods with no arguments or a single untyped argument. Calling
1237 such methods is more efficient than calling a corresponding method
1238 that uses
\constant{METH_VARARGS
}.
1239 Also, the old
\constant{METH_OLDARGS
} style of writing C methods is
1240 now officially deprecated.
1243 Two new wrapper functions,
\cfunction{PyOS_snprintf()
} and
1244 \cfunction{PyOS_vsnprintf()
} were added to provide
1245 cross-platform implementations for the relatively new
1246 \cfunction{snprintf()
} and
\cfunction{vsnprintf()
} C lib APIs. In
1247 contrast to the standard
\cfunction{sprintf()
} and
1248 \cfunction{vsprintf()
} functions, the Python versions check the
1249 bounds of the buffer used to protect against buffer overruns.
1250 (Contributed by M.-A. Lemburg.)
1252 \item The
\cfunction{_PyTuple_Resize()
} function has lost an unused
1253 parameter, so now it takes
2 parameters instead of
3. The third
1254 argument was never used, and can simply be discarded when porting
1255 code from earlier versions to Python
2.2.
1260 %======================================================================
1261 \section{Other Changes and Fixes
}
1263 % XXX update the patch and bug figures as we go
1264 As usual there were a bunch of other improvements and bugfixes
1265 scattered throughout the source tree. A search through the CVS change
1266 logs finds there were
312 patches applied, and
391 bugs fixed; both
1267 figures are likely to be underestimates. Some of the more notable
1272 \item The code for the MacOS port for Python, maintained by Jack
1273 Jansen, is now kept in the main Python CVS tree, and many changes
1274 have been made to support MacOS~X.
1276 The most significant change is the ability to build Python as a
1277 framework, enabled by supplying the
\longprogramopt{enable-framework
}
1278 option to the configure script when compiling Python. According to
1279 Jack Jansen, ``This installs a self-contained Python installation plus
1280 the OS~X framework "glue" into
1281 \file{/Library/Frameworks/Python.framework
} (or another location of
1282 choice). For now there is little immediate added benefit to this
1283 (actually, there is the disadvantage that you have to change your PATH
1284 to be able to find Python), but it is the basis for creating a
1285 full-blown Python application, porting the MacPython IDE, possibly
1286 using Python as a standard OSA scripting language and much more.''
1288 Most of the MacPython toolbox modules, which interface to MacOS APIs
1289 such as windowing, QuickTime, scripting, etc. have been ported to OS~X,
1290 but they've been left commented out in
\file{setup.py
}. People who want
1291 to experiment with these modules can uncomment them manually.
1293 % Jack's original comments:
1294 %The main change is the possibility to build Python as a
1295 %framework. This installs a self-contained Python installation plus the
1296 %OSX framework "glue" into /Library/Frameworks/Python.framework (or
1297 %another location of choice). For now there is little immedeate added
1298 %benefit to this (actually, there is the disadvantage that you have to
1299 %change your PATH to be able to find Python), but it is the basis for
1300 %creating a fullblown Python application, porting the MacPython IDE,
1301 %possibly using Python as a standard OSA scripting language and much
1302 %more. You enable this with "configure --enable-framework".
1304 %The other change is that most MacPython toolbox modules, which
1305 %interface to all the MacOS APIs such as windowing, quicktime,
1306 %scripting, etc. have been ported. Again, most of these are not of
1307 %immedeate use, as they need a full application to be really useful, so
1308 %they have been commented out in setup.py. People wanting to experiment
1309 %can uncomment them. Gestalt and Internet Config modules are enabled by
1312 \item Keyword arguments passed to builtin functions that don't take them
1313 now cause a
\exception{TypeError
} exception to be raised, with the
1314 message "
\var{function
} takes no keyword arguments".
1316 \item Weak references, added in Python
2.1 as an extension module,
1317 are now part of the core because they're used in the implementation
1318 of new-style classes. The
\exception{ReferenceError
} exception has
1319 therefore moved from the
\module{weakref
} module to become a
1322 \item A new script,
\file{Tools/scripts/cleanfuture.py
} by Tim
1323 Peters, automatically removes obsolete
\code{__future__
} statements
1324 from Python source code.
1326 \item An additional
\var{flags
} argument has been added to the
1327 built-in function
\function{compile()
}, so the behaviour of
1328 \code{__future__
} statements can now be correctly observed in
1329 simulated shells, such as those presented by IDLE and other
1330 development environments. This is described in
\pep{264}.
1331 (Contributed by Michael Hudson.)
1333 \item The new license introduced with Python
1.6 wasn't
1334 GPL-compatible. This is fixed by some minor textual changes to the
1335 2.2 license, so it's now legal to embed Python inside a GPLed
1336 program again. Note that Python itself is not GPLed, but instead is
1337 under a license that's essentially equivalent to the BSD license,
1338 same as it always was. The license changes were also applied to the
1339 Python
2.0.1 and
2.1.1 releases.
1341 \item When presented with a Unicode filename on Windows, Python will
1342 now convert it to an MBCS encoded string, as used by the Microsoft
1343 file APIs. As MBCS is explicitly used by the file APIs, Python's
1344 choice of ASCII as the default encoding turns out to be an
1345 annoyance. On Unix, the locale's character set is used if
1346 \function{locale.nl_langinfo(CODESET)
} is available. (Windows
1347 support was contributed by Mark Hammond with assistance from
1348 Marc-Andr\'e Lemburg. Unix support was added by Martin von L\"owis.)
1350 \item Large file support is now enabled on Windows. (Contributed by
1353 \item The
\file{Tools/scripts/ftpmirror.py
} script
1354 now parses a
\file{.netrc
} file, if you have one.
1355 (Contributed by Mike Romberg.)
1357 \item Some features of the object returned by the
1358 \function{xrange()
} function are now deprecated, and trigger
1359 warnings when they're accessed; they'll disappear in Python
2.3.
1360 \class{xrange
} objects tried to pretend they were full sequence
1361 types by supporting slicing, sequence multiplication, and the
1362 \keyword{in
} operator, but these features were rarely used and
1363 therefore buggy. The
\method{tolist()
} method and the
1364 \member{start
},
\member{stop
}, and
\member{step
} attributes are also
1365 being deprecated. At the C level, the fourth argument to the
1366 \cfunction{PyRange_New()
} function,
\samp{repeat
}, has also been
1369 \item There were a bunch of patches to the dictionary
1370 implementation, mostly to fix potential core dumps if a dictionary
1371 contains objects that sneakily changed their hash value, or mutated
1372 the dictionary they were contained in. For a while python-dev fell
1373 into a gentle rhythm of Michael Hudson finding a case that dumped
1374 core, Tim Peters fixing the bug, Michael finding another case, and round
1377 \item On Windows, Python can now be compiled with Borland C thanks
1378 to a number of patches contributed by Stephen Hansen, though the
1379 result isn't fully functional yet. (But this
\emph{is
} progress...)
1381 \item Another Windows enhancement: Wise Solutions generously offered
1382 PythonLabs use of their InstallerMaster
8.1 system. Earlier
1383 PythonLabs Windows installers used Wise
5.0a, which was beginning to
1384 show its age. (Packaged up by Tim Peters.)
1386 \item Files ending in
\samp{.pyw
} can now be imported on Windows.
1387 \samp{.pyw
} is a Windows-only thing, used to indicate that a script
1388 needs to be run using PYTHONW.EXE instead of PYTHON.EXE in order to
1389 prevent a DOS console from popping up to display the output. This
1390 patch makes it possible to import such scripts, in case they're also
1391 usable as modules. (Implemented by David Bolen.)
1393 \item On platforms where Python uses the C
\cfunction{dlopen()
} function
1394 to load extension modules, it's now possible to set the flags used
1395 by
\cfunction{dlopen()
} using the
\function{sys.getdlopenflags()
} and
1396 \function{sys.setdlopenflags()
} functions. (Contributed by Bram Stolk.)
1398 \item The
\function{pow()
} built-in function no longer supports
3
1399 arguments when floating-point numbers are supplied.
1400 \code{pow(
\var{x
},
\var{y
},
\var{z
})
} returns
\code{(x**y) \% z
}, but
1401 this is never useful for floating point numbers, and the final
1402 result varies unpredictably depending on the platform. A call such
1403 as
\code{pow(
2.0,
8.0,
7.0)
} will now raise a
\exception{TypeError
}
1409 %======================================================================
1410 \section{Acknowledgements
}
1412 The author would like to thank the following people for offering
1413 suggestions, corrections and assistance with various drafts of this
1414 article: Fred Bremmer, Keith Briggs, Andrew Dalke, Fred~L. Drake, Jr.,
1415 Carel Fellinger, David Goodger, Mark Hammond, Stephen Hansen, Michael
1416 Hudson, Jack Jansen, Marc-Andr\'e Lemburg, Martin von L\"owis, Fredrik
1417 Lundh, Michael McLay, Nick Mathewson, Paul Moore, Gustavo Niemeyer,
1418 Don O'Donnell, Tim Peters, Jens Quade, Tom Reinhardt, Neil
1419 Schemenauer, Guido van Rossum, Greg Ward.