blob: e32e2ab1ec4870cf5744c8377f4265b8fdd50771 [file] [log] [blame]
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +00001\documentclass{howto}
2
3% $Id$
4
5\title{What's New in Python 2.2}
Andrew M. Kuchling71dd7902002-11-12 18:45:46 +00006\release{1.02}
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +00007\author{A.M. Kuchling}
Andrew M. Kuchling5ef2b212002-11-27 18:53:38 +00008\authoraddress{\email{amk@amk.ca}}
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +00009\begin{document}
10\maketitle\tableofcontents
11
12\section{Introduction}
13
Andrew M. Kuchlingcab94a12002-11-12 18:59:20 +000014This article explains the new features in Python 2.2.2, released on
15October 14, 2002. Python 2.2.2 is a bugfix release of Python 2.2,
Andrew M. Kuchling9da3efd2002-04-01 19:22:34 +000016originally released on December 21, 2001.
Andrew M. Kuchling26c39bf2001-09-10 03:20:53 +000017
18Python 2.2 can be thought of as the "cleanup release". There are some
19features such as generators and iterators that are completely new, but
20most of the changes, significant and far-reaching though they may be,
21are aimed at cleaning up irregularities and dark corners of the
22language design.
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +000023
Andrew M. Kuchling1497b622001-09-24 14:51:16 +000024This article doesn't attempt to provide a complete specification of
Andrew M. Kuchling26c39bf2001-09-10 03:20:53 +000025the new features, but instead provides a convenient overview. For
26full details, you should refer to the documentation for Python 2.2,
Fred Drake0d002542001-07-17 13:55:33 +000027such as the
Andrew M. Kuchling5e08d102001-12-20 16:33:45 +000028\citetitle[http://www.python.org/doc/2.2/lib/lib.html]{Python
Fred Drake0d002542001-07-17 13:55:33 +000029Library Reference} and the
Andrew M. Kuchling5e08d102001-12-20 16:33:45 +000030\citetitle[http://www.python.org/doc/2.2/ref/ref.html]{Python
31Reference Manual}. If you want to understand the complete
32implementation and design rationale for a change, refer to the PEP for
33a particular new feature.
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +000034
Andrew M. Kuchling1497b622001-09-24 14:51:16 +000035\begin{seealso}
36
Andrew M. Kuchling2dab9c72001-10-31 13:16:10 +000037\seeurl{http://www.unixreview.com/documents/s=1356/urm0109h/0109h.htm}
Andrew M. Kuchling1497b622001-09-24 14:51:16 +000038{``What's So Special About Python 2.2?'' is also about the new 2.2
39features, and was written by Cameron Laird and Kathryn Soraiz.}
40
41\end{seealso}
42
Andrew M. Kuchling8cfa9052001-07-19 01:19:59 +000043
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +000044%======================================================================
Andrew M. Kuchling433b5c42001-10-30 21:36:04 +000045\section{PEPs 252 and 253: Type and Class Changes}
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +000046
Andrew M. Kuchling279e7442001-10-22 02:03:40 +000047The largest and most far-reaching changes in Python 2.2 are to
48Python's model of objects and classes. The changes should be backward
49compatible, so it's likely that your code will continue to run
50unchanged, but the changes provide some amazing new capabilities.
51Before beginning this, the longest and most complicated section of
52this article, I'll provide an overview of the changes and offer some
53comments.
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +000054
Andrew M. Kuchling279e7442001-10-22 02:03:40 +000055A long time ago I wrote a Web page
56(\url{http://www.amk.ca/python/writing/warts.html}) listing flaws in
57Python's design. One of the most significant flaws was that it's
58impossible to subclass Python types implemented in C. In particular,
59it's not possible to subclass built-in types, so you can't just
60subclass, say, lists in order to add a single useful method to them.
61The \module{UserList} module provides a class that supports all of the
62methods of lists and that can be subclassed further, but there's lots
63of C code that expects a regular Python list and won't accept a
64\class{UserList} instance.
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +000065
Andrew M. Kuchling279e7442001-10-22 02:03:40 +000066Python 2.2 fixes this, and in the process adds some exciting new
67capabilities. A brief summary:
Andrew M. Kuchling26c39bf2001-09-10 03:20:53 +000068
Andrew M. Kuchling279e7442001-10-22 02:03:40 +000069\begin{itemize}
Andrew M. Kuchlingd6e40e22001-09-10 16:18:50 +000070
Andrew M. Kuchling279e7442001-10-22 02:03:40 +000071\item You can subclass built-in types such as lists and even integers,
72and your subclasses should work in every place that requires the
73original type.
74
75\item It's now possible to define static and class methods, in addition
76to the instance methods available in previous versions of Python.
77
78\item It's also possible to automatically call methods on accessing or
79setting an instance attribute by using a new mechanism called
80\dfn{properties}. Many uses of \method{__getattr__} can be rewritten
81to use properties instead, making the resulting code simpler and
82faster. As a small side benefit, attributes can now have docstrings,
83too.
84
85\item The list of legal attributes for an instance can be limited to a
86particular set using \dfn{slots}, making it possible to safeguard
87against typos and perhaps make more optimizations possible in future
88versions of Python.
89
90\end{itemize}
91
92Some users have voiced concern about all these changes. Sure, they
93say, the new features are neat and lend themselves to all sorts of
94tricks that weren't possible in previous versions of Python, but
95they also make the language more complicated. Some people have said
96that they've always recommended Python for its simplicity, and feel
97that its simplicity is being lost.
98
99Personally, I think there's no need to worry. Many of the new
100features are quite esoteric, and you can write a lot of Python code
101without ever needed to be aware of them. Writing a simple class is no
102more difficult than it ever was, so you don't need to bother learning
103or teaching them unless they're actually needed. Some very
104complicated tasks that were previously only possible from C will now
105be possible in pure Python, and to my mind that's all for the better.
106
107I'm not going to attempt to cover every single corner case and small
108change that were required to make the new features work. Instead this
Andrew M. Kuchlingcf31d5d2001-10-26 20:37:55 +0000109section will paint only the broad strokes. See section~\ref{sect-rellinks},
Andrew M. Kuchling279e7442001-10-22 02:03:40 +0000110``Related Links'', for further sources of information about Python 2.2's new
111object model.
112
113
114\subsection{Old and New Classes}
115
116First, you should know that Python 2.2 really has two kinds of
117classes: classic or old-style classes, and new-style classes. The
118old-style class model is exactly the same as the class model in
119earlier versions of Python. All the new features described in this
120section apply only to new-style classes. This divergence isn't
121intended to last forever; eventually old-style classes will be
122dropped, possibly in Python 3.0.
123
Andrew M. Kuchling4855b022001-10-23 20:26:16 +0000124So how do you define a new-style class? You do it by subclassing an
125existing new-style class. Most of Python's built-in types, such as
126integers, lists, dictionaries, and even files, are new-style classes
127now. A new-style class named \class{object}, the base class for all
Andrew M. Kuchling71dd7902002-11-12 18:45:46 +0000128built-in types, has also been added so if no built-in type is
Andrew M. Kuchling4855b022001-10-23 20:26:16 +0000129suitable, you can just subclass \class{object}:
130
131\begin{verbatim}
132class C(object):
133 def __init__ (self):
134 ...
135 ...
136\end{verbatim}
137
138This means that \keyword{class} statements that don't have any base
Andrew M. Kuchlingcf31d5d2001-10-26 20:37:55 +0000139classes are always classic classes in Python 2.2. (Actually you can
140also change this by setting a module-level variable named
141\member{__metaclass__} --- see \pep{253} for the details --- but it's
142easier to just subclass \keyword{object}.)
Andrew M. Kuchling4855b022001-10-23 20:26:16 +0000143
144The type objects for the built-in types are available as built-ins,
145named using a clever trick. Python has always had built-in functions
146named \function{int()}, \function{float()}, and \function{str()}. In
1472.2, they aren't functions any more, but type objects that behave as
148factories when called.
149
150\begin{verbatim}
151>>> int
152<type 'int'>
153>>> int('123')
154123
155\end{verbatim}
156
157To make the set of types complete, new type objects such as
Andrew M. Kuchling1117d932001-10-29 20:37:47 +0000158\function{dict} and \function{file} have been added. Here's a
Andrew M. Kuchlingb83769c2001-10-26 20:07:03 +0000159more interesting example, adding a \method{lock()} method to file
160objects:
Andrew M. Kuchling4855b022001-10-23 20:26:16 +0000161
162\begin{verbatim}
Andrew M. Kuchlingb83769c2001-10-26 20:07:03 +0000163class LockableFile(file):
164 def lock (self, operation, length=0, start=0, whence=0):
165 import fcntl
166 return fcntl.lockf(self.fileno(), operation,
167 length, start, whence)
Andrew M. Kuchling4855b022001-10-23 20:26:16 +0000168\end{verbatim}
169
Andrew M. Kuchlingb83769c2001-10-26 20:07:03 +0000170The now-obsolete \module{posixfile} module contained a class that
171emulated all of a file object's methods and also added a
172\method{lock()} method, but this class couldn't be passed to internal
173functions that expected a built-in file, something which is possible
174with our new \class{LockableFile}.
Andrew M. Kuchling279e7442001-10-22 02:03:40 +0000175
176
177\subsection{Descriptors}
178
179In previous versions of Python, there was no consistent way to
180discover what attributes and methods were supported by an object.
181There were some informal conventions, such as defining
182\member{__members__} and \member{__methods__} attributes that were
183lists of names, but often the author of an extension type or a class
184wouldn't bother to define them. You could fall back on inspecting the
185\member{__dict__} of an object, but when class inheritance or an
186arbitrary \method{__getattr__} hook were in use this could still be
187inaccurate.
188
189The one big idea underlying the new class model is that an API for
190describing the attributes of an object using \dfn{descriptors} has
191been formalized. Descriptors specify the value of an attribute,
192stating whether it's a method or a field. With the descriptor API,
193static methods and class methods become possible, as well as more
194exotic constructs.
195
196Attribute descriptors are objects that live inside class objects, and
197have a few attributes of their own:
198
199\begin{itemize}
200
201\item \member{__name__} is the attribute's name.
202
203\item \member{__doc__} is the attribute's docstring.
204
Andrew M. Kuchling9455df22001-12-03 20:55:37 +0000205\item \method{__get__(\var{object})} is a method that retrieves the
206attribute value from \var{object}.
Andrew M. Kuchling279e7442001-10-22 02:03:40 +0000207
Andrew M. Kuchling4f9e2202001-10-29 18:09:42 +0000208\item \method{__set__(\var{object}, \var{value})} sets the attribute
Andrew M. Kuchling279e7442001-10-22 02:03:40 +0000209on \var{object} to \var{value}.
210
Andrew M. Kuchlingc54fc312001-12-03 20:58:29 +0000211\item \method{__delete__(\var{object}, \var{value})} deletes the \var{value}
Andrew M. Kuchling9455df22001-12-03 20:55:37 +0000212attribute of \var{object}.
Andrew M. Kuchling279e7442001-10-22 02:03:40 +0000213\end{itemize}
214
215For example, when you write \code{obj.x}, the steps that Python
216actually performs are:
217
218\begin{verbatim}
219descriptor = obj.__class__.x
Andrew M. Kuchling7cc13de2001-10-30 14:22:11 +0000220descriptor.__get__(obj)
Andrew M. Kuchling279e7442001-10-22 02:03:40 +0000221\end{verbatim}
222
Andrew M. Kuchling7cc13de2001-10-30 14:22:11 +0000223For methods, \method{descriptor.__get__} returns a temporary object that's
Andrew M. Kuchling279e7442001-10-22 02:03:40 +0000224callable, and wraps up the instance and the method to be called on it.
225This is also why static methods and class methods are now possible;
226they have descriptors that wrap up just the method, or the method and
227the class. As a brief explanation of these new kinds of methods,
228static methods aren't passed the instance, and therefore resemble
229regular functions. Class methods are passed the class of the object,
Andrew M. Kuchling72a7fb72001-10-30 22:18:21 +0000230but not the object itself. Static and class methods are defined like
Andrew M. Kuchling279e7442001-10-22 02:03:40 +0000231this:
232
233\begin{verbatim}
Andrew M. Kuchlingccf04652001-11-26 18:15:44 +0000234class C(object):
Andrew M. Kuchling279e7442001-10-22 02:03:40 +0000235 def f(arg1, arg2):
236 ...
237 f = staticmethod(f)
238
239 def g(cls, arg1, arg2):
240 ...
241 g = classmethod(g)
242\end{verbatim}
243
244The \function{staticmethod()} function takes the function
245\function{f}, and returns it wrapped up in a descriptor so it can be
246stored in the class object. You might expect there to be special
247syntax for creating such methods (\code{def static f()},
248\code{defstatic f()}, or something like that) but no such syntax has
Andrew M. Kuchlingbec5b362001-12-21 04:39:11 +0000249been defined yet; that's been left for future versions of Python.
Andrew M. Kuchling279e7442001-10-22 02:03:40 +0000250
251More new features, such as slots and properties, are also implemented
252as new kinds of descriptors, and it's not difficult to write a
253descriptor class that does something novel. For example, it would be
254possible to write a descriptor class that made it possible to write
255Eiffel-style preconditions and postconditions for a method. A class
256that used this feature might be defined like this:
257
258\begin{verbatim}
259from eiffel import eiffelmethod
260
Andrew M. Kuchlingccf04652001-11-26 18:15:44 +0000261class C(object):
Andrew M. Kuchling279e7442001-10-22 02:03:40 +0000262 def f(self, arg1, arg2):
263 # The actual function
Andrew M. Kuchlingbec5b362001-12-21 04:39:11 +0000264 ...
Andrew M. Kuchling279e7442001-10-22 02:03:40 +0000265 def pre_f(self):
266 # Check preconditions
Andrew M. Kuchlingbec5b362001-12-21 04:39:11 +0000267 ...
Andrew M. Kuchling279e7442001-10-22 02:03:40 +0000268 def post_f(self):
269 # Check postconditions
Andrew M. Kuchlingbec5b362001-12-21 04:39:11 +0000270 ...
Andrew M. Kuchling279e7442001-10-22 02:03:40 +0000271
272 f = eiffelmethod(f, pre_f, post_f)
273\end{verbatim}
274
275Note that a person using the new \function{eiffelmethod()} doesn't
276have to understand anything about descriptors. This is why I think
277the new features don't increase the basic complexity of the language.
278There will be a few wizards who need to know about it in order to
279write \function{eiffelmethod()} or the ZODB or whatever, but most
280users will just write code on top of the resulting libraries and
281ignore the implementation details.
282
Andrew M. Kuchlingbec5b362001-12-21 04:39:11 +0000283
Andrew M. Kuchling4855b022001-10-23 20:26:16 +0000284\subsection{Multiple Inheritance: The Diamond Rule}
Andrew M. Kuchling279e7442001-10-22 02:03:40 +0000285
Andrew M. Kuchling4855b022001-10-23 20:26:16 +0000286Multiple inheritance has also been made more useful through changing
287the rules under which names are resolved. Consider this set of classes
288(diagram taken from \pep{253} by Guido van Rossum):
289
290\begin{verbatim}
291 class A:
292 ^ ^ def save(self): ...
293 / \
294 / \
295 / \
296 / \
297 class B class C:
298 ^ ^ def save(self): ...
299 \ /
300 \ /
301 \ /
302 \ /
303 class D
304\end{verbatim}
305
306The lookup rule for classic classes is simple but not very smart; the
307base classes are searched depth-first, going from left to right. A
308reference to \method{D.save} will search the classes \class{D},
309\class{B}, and then \class{A}, where \method{save()} would be found
310and returned. \method{C.save()} would never be found at all. This is
311bad, because if \class{C}'s \method{save()} method is saving some
312internal state specific to \class{C}, not calling it will result in
313that state never getting saved.
314
315New-style classes follow a different algorithm that's a bit more
316complicated to explain, but does the right thing in this situation.
Andrew M. Kuchling88eed702002-11-15 14:37:46 +0000317(Note that Python 2.3 changes this algorithm to one that produces the
318same results in most cases, but produces more useful results for
319really complicated inheritance graphs.)
Andrew M. Kuchling4855b022001-10-23 20:26:16 +0000320
321\begin{enumerate}
322
323\item List all the base classes, following the classic lookup rule and
324include a class multiple times if it's visited repeatedly. In the
325above example, the list of visited classes is [\class{D}, \class{B},
Andrew M. Kuchling28369072001-10-29 15:47:33 +0000326\class{A}, \class{C}, \class{A}].
Andrew M. Kuchling4855b022001-10-23 20:26:16 +0000327
328\item Scan the list for duplicated classes. If any are found, remove
329all but one occurrence, leaving the \emph{last} one in the list. In
330the above example, the list becomes [\class{D}, \class{B}, \class{C},
Andrew M. Kuchling28369072001-10-29 15:47:33 +0000331\class{A}] after dropping duplicates.
Andrew M. Kuchling4855b022001-10-23 20:26:16 +0000332
333\end{enumerate}
334
335Following this rule, referring to \method{D.save()} will return
336\method{C.save()}, which is the behaviour we're after. This lookup
Andrew M. Kuchlingbec5b362001-12-21 04:39:11 +0000337rule is the same as the one followed by Common Lisp. A new built-in
338function, \function{super()}, provides a way to get at a class's
339superclasses without having to reimplement Python's algorithm.
340The most commonly used form will be
341\function{super(\var{class}, \var{obj})}, which returns
342a bound superclass object (not the actual class object). This form
343will be used in methods to call a method in the superclass; for
344example, \class{D}'s \method{save()} method would look like this:
Andrew M. Kuchling4855b022001-10-23 20:26:16 +0000345
Andrew M. Kuchlingbec5b362001-12-21 04:39:11 +0000346\begin{verbatim}
347class D:
348 def save (self):
349 # Call superclass .save()
350 super(D, self).save()
351 # Save D's private information here
352 ...
353\end{verbatim}
354
355\function{super()} can also return unbound superclass objects
356when called as \function{super(\var{class})} or
357\function{super(\var{class1}, \var{class2})}, but this probably won't
358often be useful.
Andrew M. Kuchling433b5c42001-10-30 21:36:04 +0000359
Andrew M. Kuchling279e7442001-10-22 02:03:40 +0000360
361\subsection{Attribute Access}
362
Andrew M. Kuchlingb83769c2001-10-26 20:07:03 +0000363A fair number of sophisticated Python classes define hooks for
364attribute access using \method{__getattr__}; most commonly this is
365done for convenience, to make code more readable by automatically
366mapping an attribute access such as \code{obj.parent} into a method
367call such as \code{obj.get_parent()}. Python 2.2 adds some new ways
368of controlling attribute access.
Andrew M. Kuchling279e7442001-10-22 02:03:40 +0000369
Andrew M. Kuchlingb83769c2001-10-26 20:07:03 +0000370First, \method{__getattr__(\var{attr_name})} is still supported by
371new-style classes, and nothing about it has changed. As before, it
372will be called when an attempt is made to access \code{obj.foo} and no
373attribute named \samp{foo} is found in the instance's dictionary.
374
375New-style classes also support a new method,
376\method{__getattribute__(\var{attr_name})}. The difference between
377the two methods is that \method{__getattribute__} is \emph{always}
378called whenever any attribute is accessed, while the old
379\method{__getattr__} is only called if \samp{foo} isn't found in the
380instance's dictionary.
381
382However, Python 2.2's support for \dfn{properties} will often be a
383simpler way to trap attribute references. Writing a
384\method{__getattr__} method is complicated because to avoid recursion
385you can't use regular attribute accesses inside them, and instead have
386to mess around with the contents of \member{__dict__}.
387\method{__getattr__} methods also end up being called by Python when
388it checks for other methods such as \method{__repr__} or
389\method{__coerce__}, and so have to be written with this in mind.
390Finally, calling a function on every attribute access results in a
391sizable performance loss.
392
393\class{property} is a new built-in type that packages up three
394functions that get, set, or delete an attribute, and a docstring. For
395example, if you want to define a \member{size} attribute that's
396computed, but also settable, you could write:
397
398\begin{verbatim}
Andrew M. Kuchlingccf04652001-11-26 18:15:44 +0000399class C(object):
Andrew M. Kuchlingb83769c2001-10-26 20:07:03 +0000400 def get_size (self):
401 result = ... computation ...
402 return result
403 def set_size (self, size):
404 ... compute something based on the size
405 and set internal state appropriately ...
406
407 # Define a property. The 'delete this attribute'
408 # method is defined as None, so the attribute
409 # can't be deleted.
410 size = property(get_size, set_size,
411 None,
412 "Storage size of this instance")
413\end{verbatim}
414
415That is certainly clearer and easier to write than a pair of
416\method{__getattr__}/\method{__setattr__} methods that check for the
Andrew M. Kuchling7aa63c22001-10-30 14:35:03 +0000417\member{size} attribute and handle it specially while retrieving all
Andrew M. Kuchlingb83769c2001-10-26 20:07:03 +0000418other attributes from the instance's \member{__dict__}. Accesses to
419\member{size} are also the only ones which have to perform the work of
Andrew M. Kuchling7aa63c22001-10-30 14:35:03 +0000420calling a function, so references to other attributes run at
Andrew M. Kuchlingb83769c2001-10-26 20:07:03 +0000421their usual speed.
422
423Finally, it's possible to constrain the list of attributes that can be
Andrew M. Kuchling7aa63c22001-10-30 14:35:03 +0000424referenced on an object using the new \member{__slots__} class attribute.
Andrew M. Kuchlingb83769c2001-10-26 20:07:03 +0000425Python objects are usually very dynamic; at any time it's possible to
426define a new attribute on an instance by just doing
427\code{obj.new_attr=1}. This is flexible and convenient, but this
428flexibility can also lead to bugs, as when you meant to write
Andrew M. Kuchling7aa63c22001-10-30 14:35:03 +0000429\code{obj.template = 'a'} but made a typo and wrote
Andrew M. Kuchlingb83769c2001-10-26 20:07:03 +0000430\code{obj.templtae} by accident.
431
Andrew M. Kuchling7aa63c22001-10-30 14:35:03 +0000432A new-style class can define a class attribute named \member{__slots__}
Andrew M. Kuchlingb83769c2001-10-26 20:07:03 +0000433to constrain the list of legal attribute names. An example will make
434this clear:
435
436\begin{verbatim}
437>>> class C(object):
Andrew M. Kuchling038215a2001-12-07 14:22:13 +0000438... __slots__ = ('template', 'name')
Andrew M. Kuchlingb83769c2001-10-26 20:07:03 +0000439...
440>>> obj = C()
441>>> print obj.template
442None
Andrew M. Kuchling28369072001-10-29 15:47:33 +0000443>>> obj.template = 'Test'
Andrew M. Kuchlingb83769c2001-10-26 20:07:03 +0000444>>> print obj.template
445Test
Andrew M. Kuchling28369072001-10-29 15:47:33 +0000446>>> obj.templtae = None
Andrew M. Kuchlingb83769c2001-10-26 20:07:03 +0000447Traceback (most recent call last):
448 File "<stdin>", line 1, in ?
449AttributeError: 'C' object has no attribute 'templtae'
450\end{verbatim}
451
Andrew M. Kuchling7aa63c22001-10-30 14:35:03 +0000452Note how you get an \exception{AttributeError} on the attempt to
453assign to an attribute not listed in \member{__slots__}.
Andrew M. Kuchling4855b022001-10-23 20:26:16 +0000454
455
Andrew M. Kuchling279e7442001-10-22 02:03:40 +0000456\subsection{Related Links}
Andrew M. Kuchlingcf31d5d2001-10-26 20:37:55 +0000457\label{sect-rellinks}
Andrew M. Kuchling279e7442001-10-22 02:03:40 +0000458
459This section has just been a quick overview of the new features,
460giving enough of an explanation to start you programming, but many
461details have been simplified or ignored. Where should you go to get a
462more complete picture?
463
Andrew M. Kuchlingb83769c2001-10-26 20:07:03 +0000464\url{http://www.python.org/2.2/descrintro.html} is a lengthy tutorial
Andrew M. Kuchling279e7442001-10-22 02:03:40 +0000465introduction to the descriptor features, written by Guido van Rossum.
Andrew M. Kuchlingb83769c2001-10-26 20:07:03 +0000466If my description has whetted your appetite, go read this tutorial
467next, because it goes into much more detail about the new features
468while still remaining quite easy to read.
Andrew M. Kuchling279e7442001-10-22 02:03:40 +0000469
470Next, there are two relevant PEPs, \pep{252} and \pep{253}. \pep{252}
471is titled "Making Types Look More Like Classes", and covers the
472descriptor API. \pep{253} is titled "Subtyping Built-in Types", and
473describes the changes to type objects that make it possible to subtype
Andrew M. Kuchlingb83769c2001-10-26 20:07:03 +0000474built-in objects. \pep{253} is the more complicated PEP of the two,
475and at a few points the necessary explanations of types and meta-types
476may cause your head to explode. Both PEPs were written and
477implemented by Guido van Rossum, with substantial assistance from the
478rest of the Zope Corp. team.
Andrew M. Kuchling279e7442001-10-22 02:03:40 +0000479
Andrew M. Kuchlingb83769c2001-10-26 20:07:03 +0000480Finally, there's the ultimate authority: the source code. Most of the
481machinery for the type handling is in \file{Objects/typeobject.c}, but
482you should only resort to it after all other avenues have been
Andrew M. Kuchling7aa63c22001-10-30 14:35:03 +0000483exhausted, including posting a question to python-list or python-dev.
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +0000484
Andrew M. Kuchling8cfa9052001-07-19 01:19:59 +0000485
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +0000486%======================================================================
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000487\section{PEP 234: Iterators}
488
Andrew M. Kuchling7aa63c22001-10-30 14:35:03 +0000489Another significant addition to 2.2 is an iteration interface at both
490the C and Python levels. Objects can define how they can be looped
491over by callers.
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000492
493In Python versions up to 2.1, the usual way to make \code{for item in
494obj} work is to define a \method{__getitem__()} method that looks
495something like this:
496
497\begin{verbatim}
498 def __getitem__(self, index):
499 return <next item>
500\end{verbatim}
501
502\method{__getitem__()} is more properly used to define an indexing
503operation on an object so that you can write \code{obj[5]} to retrieve
Andrew M. Kuchling8c69c91b2001-08-07 14:28:58 +0000504the sixth element. It's a bit misleading when you're using this only
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000505to support \keyword{for} loops. Consider some file-like object that
506wants to be looped over; the \var{index} parameter is essentially
507meaningless, as the class probably assumes that a series of
Andrew M. Kuchling7aa63c22001-10-30 14:35:03 +0000508\method{__getitem__()} calls will be made with \var{index}
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000509incrementing by one each time. In other words, the presence of the
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000510\method{__getitem__()} method doesn't mean that using \code{file[5]}
511to randomly access the sixth element will work, though it really should.
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000512
513In Python 2.2, iteration can be implemented separately, and
514\method{__getitem__()} methods can be limited to classes that really
Andrew M. Kuchling7aa63c22001-10-30 14:35:03 +0000515do support random access. The basic idea of iterators is
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000516simple. A new built-in function, \function{iter(obj)} or
517\code{iter(\var{C}, \var{sentinel})}, is used to get an iterator.
518\function{iter(obj)} returns an iterator for the object \var{obj},
519while \code{iter(\var{C}, \var{sentinel})} returns an iterator that
520will invoke the callable object \var{C} until it returns
521\var{sentinel} to signal that the iterator is done.
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000522
523Python classes can define an \method{__iter__()} method, which should
524create and return a new iterator for the object; if the object is its
525own iterator, this method can just return \code{self}. In particular,
526iterators will usually be their own iterators. Extension types
Fred Drakea030c762002-05-02 17:55:26 +0000527implemented in C can implement a \member{tp_iter} function in order to
Andrew M. Kuchling4cf52a92001-07-17 12:48:48 +0000528return an iterator, and extension types that want to behave as
Fred Drakea030c762002-05-02 17:55:26 +0000529iterators can define a \member{tp_iternext} function.
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000530
Andrew M. Kuchling7aa63c22001-10-30 14:35:03 +0000531So, after all this, what do iterators actually do? They have one
532required method, \method{next()}, which takes no arguments and returns
533the next value. When there are no more values to be returned, calling
534\method{next()} should raise the \exception{StopIteration} exception.
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000535
536\begin{verbatim}
537>>> L = [1,2,3]
538>>> i = iter(L)
539>>> print i
540<iterator object at 0x8116870>
541>>> i.next()
5421
543>>> i.next()
5442
545>>> i.next()
5463
547>>> i.next()
548Traceback (most recent call last):
549 File "<stdin>", line 1, in ?
550StopIteration
551>>>
552\end{verbatim}
553
554In 2.2, Python's \keyword{for} statement no longer expects a sequence;
Andrew M. Kuchling7aa63c22001-10-30 14:35:03 +0000555it expects something for which \function{iter()} will return an iterator.
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000556For backward compatibility and convenience, an iterator is
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000557automatically constructed for sequences that don't implement
Fred Drakea030c762002-05-02 17:55:26 +0000558\method{__iter__()} or a \member{tp_iter} slot, so \code{for i in
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000559[1,2,3]} will still work. Wherever the Python interpreter loops over
560a sequence, it's been changed to use the iterator protocol. This
561means you can do things like this:
562
563\begin{verbatim}
Andrew M. Kuchling7aa63c22001-10-30 14:35:03 +0000564>>> L = [1,2,3]
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000565>>> i = iter(L)
566>>> a,b,c = i
567>>> a,b,c
568(1, 2, 3)
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000569\end{verbatim}
570
Andrew M. Kuchling9e9c1352001-08-11 03:06:50 +0000571Iterator support has been added to some of Python's basic types.
Fred Drake0d002542001-07-17 13:55:33 +0000572Calling \function{iter()} on a dictionary will return an iterator
Andrew M. Kuchling6ea9f0b2001-07-17 14:50:31 +0000573which loops over its keys:
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000574
575\begin{verbatim}
576>>> m = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6,
577... 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12}
578>>> for key in m: print key, m[key]
579...
580Mar 3
581Feb 2
582Aug 8
583Sep 9
584May 5
585Jun 6
586Jul 7
587Jan 1
588Apr 4
589Nov 11
590Dec 12
591Oct 10
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000592\end{verbatim}
593
594That's just the default behaviour. If you want to iterate over keys,
595values, or key/value pairs, you can explicitly call the
596\method{iterkeys()}, \method{itervalues()}, or \method{iteritems()}
Andrew M. Kuchling9e9c1352001-08-11 03:06:50 +0000597methods to get an appropriate iterator. In a minor related change,
598the \keyword{in} operator now works on dictionaries, so
599\code{\var{key} in dict} is now equivalent to
600\code{dict.has_key(\var{key})}.
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000601
Andrew M. Kuchling9e9c1352001-08-11 03:06:50 +0000602Files also provide an iterator, which calls the \method{readline()}
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000603method until there are no more lines in the file. This means you can
604now read each line of a file using code like this:
605
606\begin{verbatim}
607for line in file:
608 # do something for each line
Andrew M. Kuchling7aa63c22001-10-30 14:35:03 +0000609 ...
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000610\end{verbatim}
611
612Note that you can only go forward in an iterator; there's no way to
613get the previous element, reset the iterator, or make a copy of it.
Fred Drake0d002542001-07-17 13:55:33 +0000614An iterator object could provide such additional capabilities, but the
615iterator protocol only requires a \method{next()} method.
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000616
617\begin{seealso}
618
619\seepep{234}{Iterators}{Written by Ka-Ping Yee and GvR; implemented
620by the Python Labs crew, mostly by GvR and Tim Peters.}
621
622\end{seealso}
623
Andrew M. Kuchling8cfa9052001-07-19 01:19:59 +0000624
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000625%======================================================================
626\section{PEP 255: Simple Generators}
627
628Generators are another new feature, one that interacts with the
629introduction of iterators.
630
631You're doubtless familiar with how function calls work in Python or
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000632C. When you call a function, it gets a private namespace where its local
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000633variables are created. When the function reaches a \keyword{return}
634statement, the local variables are destroyed and the resulting value
635is returned to the caller. A later call to the same function will get
636a fresh new set of local variables. But, what if the local variables
Andrew M. Kuchling7aa63c22001-10-30 14:35:03 +0000637weren't thrown away on exiting a function? What if you could later
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000638resume the function where it left off? This is what generators
639provide; they can be thought of as resumable functions.
640
641Here's the simplest example of a generator function:
642
643\begin{verbatim}
644def generate_ints(N):
645 for i in range(N):
646 yield i
647\end{verbatim}
648
649A new keyword, \keyword{yield}, was introduced for generators. Any
650function containing a \keyword{yield} statement is a generator
651function; this is detected by Python's bytecode compiler which
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000652compiles the function specially as a result. Because a new keyword was
Andrew M. Kuchling4cf52a92001-07-17 12:48:48 +0000653introduced, generators must be explicitly enabled in a module by
654including a \code{from __future__ import generators} statement near
655the top of the module's source code. In Python 2.3 this statement
656will become unnecessary.
657
658When you call a generator function, it doesn't return a single value;
659instead it returns a generator object that supports the iterator
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000660protocol. On executing the \keyword{yield} statement, the generator
Andrew M. Kuchling4cf52a92001-07-17 12:48:48 +0000661outputs the value of \code{i}, similar to a \keyword{return}
662statement. The big difference between \keyword{yield} and a
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000663\keyword{return} statement is that on reaching a \keyword{yield} the
Andrew M. Kuchling4cf52a92001-07-17 12:48:48 +0000664generator's state of execution is suspended and local variables are
Fred Drakea030c762002-05-02 17:55:26 +0000665preserved. On the next call to the generator's \code{next()} method,
Andrew M. Kuchling4cf52a92001-07-17 12:48:48 +0000666the function will resume executing immediately after the
667\keyword{yield} statement. (For complicated reasons, the
668\keyword{yield} statement isn't allowed inside the \keyword{try} block
Fred Drakea030c762002-05-02 17:55:26 +0000669of a \keyword{try}...\keyword{finally} statement; read \pep{255} for a full
Andrew M. Kuchling4cf52a92001-07-17 12:48:48 +0000670explanation of the interaction between \keyword{yield} and
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000671exceptions.)
672
673Here's a sample usage of the \function{generate_ints} generator:
674
675\begin{verbatim}
676>>> gen = generate_ints(3)
677>>> gen
678<generator object at 0x8117f90>
679>>> gen.next()
6800
681>>> gen.next()
6821
683>>> gen.next()
6842
685>>> gen.next()
686Traceback (most recent call last):
687 File "<stdin>", line 1, in ?
688 File "<stdin>", line 2, in generate_ints
689StopIteration
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000690\end{verbatim}
691
692You could equally write \code{for i in generate_ints(5)}, or
693\code{a,b,c = generate_ints(3)}.
694
695Inside a generator function, the \keyword{return} statement can only
Andrew M. Kuchling4cf52a92001-07-17 12:48:48 +0000696be used without a value, and signals the end of the procession of
697values; afterwards the generator cannot return any further values.
698\keyword{return} with a value, such as \code{return 5}, is a syntax
699error inside a generator function. The end of the generator's results
700can also be indicated by raising \exception{StopIteration} manually,
701or by just letting the flow of execution fall off the bottom of the
702function.
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000703
704You could achieve the effect of generators manually by writing your
Andrew M. Kuchling4cf52a92001-07-17 12:48:48 +0000705own class and storing all the local variables of the generator as
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000706instance variables. For example, returning a list of integers could
707be done by setting \code{self.count} to 0, and having the
708\method{next()} method increment \code{self.count} and return it.
Andrew M. Kuchlingc32cc7c2001-07-17 18:25:01 +0000709However, for a moderately complicated generator, writing a
710corresponding class would be much messier.
711\file{Lib/test/test_generators.py} contains a number of more
712interesting examples. The simplest one implements an in-order
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000713traversal of a tree using generators recursively.
714
715\begin{verbatim}
716# A recursive generator that generates Tree leaves in in-order.
717def inorder(t):
718 if t:
719 for x in inorder(t.left):
720 yield x
721 yield t.label
722 for x in inorder(t.right):
723 yield x
724\end{verbatim}
725
726Two other examples in \file{Lib/test/test_generators.py} produce
727solutions for the N-Queens problem (placing $N$ queens on an $NxN$
728chess board so that no queen threatens another) and the Knight's Tour
729(a route that takes a knight to every square of an $NxN$ chessboard
730without visiting any square twice).
731
732The idea of generators comes from other programming languages,
733especially Icon (\url{http://www.cs.arizona.edu/icon/}), where the
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000734idea of generators is central. In Icon, every
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000735expression and function call behaves like a generator. One example
736from ``An Overview of the Icon Programming Language'' at
737\url{http://www.cs.arizona.edu/icon/docs/ipd266.htm} gives an idea of
738what this looks like:
739
740\begin{verbatim}
741sentence := "Store it in the neighboring harbor"
742if (i := find("or", sentence)) > 5 then write(i)
743\end{verbatim}
744
Andrew M. Kuchling7aa63c22001-10-30 14:35:03 +0000745In Icon the \function{find()} function returns the indexes at which the
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000746substring ``or'' is found: 3, 23, 33. In the \keyword{if} statement,
747\code{i} is first assigned a value of 3, but 3 is less than 5, so the
748comparison fails, and Icon retries it with the second value of 23. 23
749is greater than 5, so the comparison now succeeds, and the code prints
750the value 23 to the screen.
751
752Python doesn't go nearly as far as Icon in adopting generators as a
753central concept. Generators are considered a new part of the core
754Python language, but learning or using them isn't compulsory; if they
755don't solve any problems that you have, feel free to ignore them.
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000756One novel feature of Python's interface as compared to
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000757Icon's is that a generator's state is represented as a concrete object
Andrew M. Kuchling7aa63c22001-10-30 14:35:03 +0000758(the iterator) that can be passed around to other functions or stored
759in a data structure.
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000760
761\begin{seealso}
762
Andrew M. Kuchling4cf52a92001-07-17 12:48:48 +0000763\seepep{255}{Simple Generators}{Written by Neil Schemenauer, Tim
764Peters, Magnus Lie Hetland. Implemented mostly by Neil Schemenauer
765and Tim Peters, with other fixes from the Python Labs crew.}
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000766
767\end{seealso}
768
Andrew M. Kuchling8cfa9052001-07-19 01:19:59 +0000769
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000770%======================================================================
Andrew M. Kuchling2f0047a2001-09-05 14:53:31 +0000771\section{PEP 237: Unifying Long Integers and Integers}
772
Andrew M. Kuchling26c39bf2001-09-10 03:20:53 +0000773In recent versions, the distinction between regular integers, which
774are 32-bit values on most machines, and long integers, which can be of
775arbitrary size, was becoming an annoyance. For example, on platforms
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000776that support files larger than \code{2**32} bytes, the
Andrew M. Kuchling26c39bf2001-09-10 03:20:53 +0000777\method{tell()} method of file objects has to return a long integer.
778However, there were various bits of Python that expected plain
779integers and would raise an error if a long integer was provided
Andrew M. Kuchlingd6e40e22001-09-10 16:18:50 +0000780instead. For example, in Python 1.5, only regular integers
Andrew M. Kuchling26c39bf2001-09-10 03:20:53 +0000781could be used as a slice index, and \code{'abc'[1L:]} would raise a
782\exception{TypeError} exception with the message 'slice index must be
783int'.
Andrew M. Kuchling2f0047a2001-09-05 14:53:31 +0000784
Andrew M. Kuchling26c39bf2001-09-10 03:20:53 +0000785Python 2.2 will shift values from short to long integers as required.
786The 'L' suffix is no longer needed to indicate a long integer literal,
787as now the compiler will choose the appropriate type. (Using the 'L'
788suffix will be discouraged in future 2.x versions of Python,
789triggering a warning in Python 2.4, and probably dropped in Python
7903.0.) Many operations that used to raise an \exception{OverflowError}
791will now return a long integer as their result. For example:
792
793\begin{verbatim}
794>>> 1234567890123
Andrew M. Kuchlingd6e40e22001-09-10 16:18:50 +00007951234567890123L
796>>> 2 ** 64
79718446744073709551616L
Andrew M. Kuchling26c39bf2001-09-10 03:20:53 +0000798\end{verbatim}
799
800In most cases, integers and long integers will now be treated
801identically. You can still distinguish them with the
Andrew M. Kuchling7aa63c22001-10-30 14:35:03 +0000802\function{type()} built-in function, but that's rarely needed.
Andrew M. Kuchling26c39bf2001-09-10 03:20:53 +0000803
Andrew M. Kuchling26c39bf2001-09-10 03:20:53 +0000804\begin{seealso}
805
806\seepep{237}{Unifying Long Integers and Integers}{Written by
Andrew M. Kuchling7aa63c22001-10-30 14:35:03 +0000807Moshe Zadka and Guido van Rossum. Implemented mostly by Guido van
808Rossum.}
Andrew M. Kuchling26c39bf2001-09-10 03:20:53 +0000809
810\end{seealso}
Andrew M. Kuchling2f0047a2001-09-05 14:53:31 +0000811
Andrew M. Kuchlingd4707e32001-09-28 20:46:46 +0000812
Andrew M. Kuchling2f0047a2001-09-05 14:53:31 +0000813%======================================================================
Andrew M. Kuchling9e9c1352001-08-11 03:06:50 +0000814\section{PEP 238: Changing the Division Operator}
815
Andrew M. Kuchling7aa63c22001-10-30 14:35:03 +0000816The most controversial change in Python 2.2 heralds the start of an effort
Andrew M. Kuchling9e9c1352001-08-11 03:06:50 +0000817to fix an old design flaw that's been in Python from the beginning.
818Currently Python's division operator, \code{/}, behaves like C's
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000819division operator when presented with two integer arguments: it
Andrew M. Kuchling9e9c1352001-08-11 03:06:50 +0000820returns an integer result that's truncated down when there would be
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000821a fractional part. For example, \code{3/2} is 1, not 1.5, and
Andrew M. Kuchling9e9c1352001-08-11 03:06:50 +0000822\code{(-1)/2} is -1, not -0.5. This means that the results of divison
823can vary unexpectedly depending on the type of the two operands and
824because Python is dynamically typed, it can be difficult to determine
825the possible types of the operands.
826
827(The controversy is over whether this is \emph{really} a design flaw,
828and whether it's worth breaking existing code to fix this. It's
Andrew M. Kuchling7aa63c22001-10-30 14:35:03 +0000829caused endless discussions on python-dev, and in July 2001 erupted into an
Andrew M. Kuchling9e9c1352001-08-11 03:06:50 +0000830storm of acidly sarcastic postings on \newsgroup{comp.lang.python}. I
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000831won't argue for either side here and will stick to describing what's
Andrew M. Kuchlingcf31d5d2001-10-26 20:37:55 +0000832implemented in 2.2. Read \pep{238} for a summary of arguments and
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000833counter-arguments.)
Andrew M. Kuchling9e9c1352001-08-11 03:06:50 +0000834
835Because this change might break code, it's being introduced very
836gradually. Python 2.2 begins the transition, but the switch won't be
837complete until Python 3.0.
838
Andrew M. Kuchlingcf31d5d2001-10-26 20:37:55 +0000839First, I'll borrow some terminology from \pep{238}. ``True division'' is the
Andrew M. Kuchling9e9c1352001-08-11 03:06:50 +0000840division that most non-programmers are familiar with: 3/2 is 1.5, 1/4
841is 0.25, and so forth. ``Floor division'' is what Python's \code{/}
842operator currently does when given integer operands; the result is the
843floor of the value returned by true division. ``Classic division'' is
844the current mixed behaviour of \code{/}; it returns the result of
845floor division when the operands are integers, and returns the result
846of true division when one of the operands is a floating-point number.
847
848Here are the changes 2.2 introduces:
849
850\begin{itemize}
851
852\item A new operator, \code{//}, is the floor division operator.
853(Yes, we know it looks like \Cpp's comment symbol.) \code{//}
Andrew M. Kuchling7aa63c22001-10-30 14:35:03 +0000854\emph{always} performs floor division no matter what the types of
Andrew M. Kuchling9e9c1352001-08-11 03:06:50 +0000855its operands are, so \code{1 // 2} is 0 and \code{1.0 // 2.0} is also
8560.0.
857
858\code{//} is always available in Python 2.2; you don't need to enable
859it using a \code{__future__} statement.
860
Andrew M. Kuchling4f9e2202001-10-29 18:09:42 +0000861\item By including a \code{from __future__ import division} in a
Andrew M. Kuchling9e9c1352001-08-11 03:06:50 +0000862module, the \code{/} operator will be changed to return the result of
863true division, so \code{1/2} is 0.5. Without the \code{__future__}
864statement, \code{/} still means classic division. The default meaning
865of \code{/} will not change until Python 3.0.
866
867\item Classes can define methods called \method{__truediv__} and
868\method{__floordiv__} to overload the two division operators. At the
Fred Drakea030c762002-05-02 17:55:26 +0000869C level, there are also slots in the \ctype{PyNumberMethods} structure
Andrew M. Kuchling9e9c1352001-08-11 03:06:50 +0000870so extension types can define the two operators.
871
Andrew M. Kuchlingcf31d5d2001-10-26 20:37:55 +0000872\item Python 2.2 supports some command-line arguments for testing
873whether code will works with the changed division semantics. Running
874python with \programopt{-Q warn} will cause a warning to be issued
875whenever division is applied to two integers. You can use this to
876find code that's affected by the change and fix it. By default,
877Python 2.2 will simply perform classic division without a warning; the
878warning will be turned on by default in Python 2.3.
Andrew M. Kuchling9e9c1352001-08-11 03:06:50 +0000879
880\end{itemize}
881
882\begin{seealso}
883
884\seepep{238}{Changing the Division Operator}{Written by Moshe Zadka and
885Guido van Rossum. Implemented by Guido van Rossum..}
886
887\end{seealso}
888
889
890%======================================================================
Andrew M. Kuchlinga43e7032001-06-27 20:32:12 +0000891\section{Unicode Changes}
892
Andrew M. Kuchling2cd712b2001-07-16 13:39:08 +0000893Python's Unicode support has been enhanced a bit in 2.2. Unicode
Andrew M. Kuchlinga6d2a042001-07-20 18:34:34 +0000894strings are usually stored as UCS-2, as 16-bit unsigned integers.
Andrew M. Kuchlingf5fec3c2001-07-19 01:48:08 +0000895Python 2.2 can also be compiled to use UCS-4, 32-bit unsigned
896integers, as its internal encoding by supplying
Andrew M. Kuchling433b5c42001-10-30 21:36:04 +0000897\longprogramopt{enable-unicode=ucs4} to the configure script.
898(It's also possible to specify
899\longprogramopt{disable-unicode} to completely disable Unicode
900support.)
901
902When built to use UCS-4 (a ``wide Python''), the interpreter can
903natively handle Unicode characters from U+000000 to U+110000, so the
904range of legal values for the \function{unichr()} function is expanded
Andrew M. Kuchlinga6d2a042001-07-20 18:34:34 +0000905accordingly. Using an interpreter compiled to use UCS-2 (a ``narrow
906Python''), values greater than 65535 will still cause
907\function{unichr()} to raise a \exception{ValueError} exception.
Andrew M. Kuchling433b5c42001-10-30 21:36:04 +0000908This is all described in \pep{261}, ``Support for `wide' Unicode
909characters''; consult it for further details.
Andrew M. Kuchlingab010872001-07-19 14:59:53 +0000910
Andrew M. Kuchling433b5c42001-10-30 21:36:04 +0000911Another change is simpler to explain. Since their introduction,
Andrew M. Kuchlingab010872001-07-19 14:59:53 +0000912Unicode strings have supported an \method{encode()} method to convert
913the string to a selected encoding such as UTF-8 or Latin-1. A
914symmetric \method{decode(\optional{\var{encoding}})} method has been
915added to 8-bit strings (though not to Unicode strings) in 2.2.
916\method{decode()} assumes that the string is in the specified encoding
917and decodes it, returning whatever is returned by the codec.
918
919Using this new feature, codecs have been added for tasks not directly
920related to Unicode. For example, codecs have been added for
921uu-encoding, MIME's base64 encoding, and compression with the
922\module{zlib} module:
Andrew M. Kuchling2cd712b2001-07-16 13:39:08 +0000923
924\begin{verbatim}
925>>> s = """Here is a lengthy piece of redundant, overly verbose,
926... and repetitive text.
927... """
928>>> data = s.encode('zlib')
929>>> data
930'x\x9c\r\xc9\xc1\r\x80 \x10\x04\xc0?Ul...'
931>>> data.decode('zlib')
932'Here is a lengthy piece of redundant, overly verbose,\nand repetitive text.\n'
933>>> print s.encode('uu')
934begin 666 <data>
935M2&5R92!I<R!A(&QE;F=T:'D@<&EE8V4@;V8@<F5D=6YD86YT+"!O=F5R;'D@
936>=F5R8F]S92P*86YD(')E<&5T:71I=F4@=&5X="X*
937
938end
939>>> "sheesh".encode('rot-13')
940'furrfu'
941\end{verbatim}
Andrew M. Kuchlinga43e7032001-06-27 20:32:12 +0000942
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000943To convert a class instance to Unicode, a \method{__unicode__} method
Andrew M. Kuchlingcf31d5d2001-10-26 20:37:55 +0000944can be defined by a class, analogous to \method{__str__}.
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000945
Andrew M. Kuchlingcf31d5d2001-10-26 20:37:55 +0000946\method{encode()}, \method{decode()}, and \method{__unicode__} were
947implemented by Marc-Andr\'e Lemburg. The changes to support using
948UCS-4 internally were implemented by Fredrik Lundh and Martin von
949L\"owis.
Andrew M. Kuchlinga43e7032001-06-27 20:32:12 +0000950
Andrew M. Kuchlingf5fec3c2001-07-19 01:48:08 +0000951\begin{seealso}
952
Andrew M. Kuchling433b5c42001-10-30 21:36:04 +0000953\seepep{261}{Support for `wide' Unicode characters}{Written by
954Paul Prescod.}
Andrew M. Kuchlingf5fec3c2001-07-19 01:48:08 +0000955
956\end{seealso}
Andrew M. Kuchling8cfa9052001-07-19 01:19:59 +0000957
Andrew M. Kuchlingcf31d5d2001-10-26 20:37:55 +0000958
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000959%======================================================================
960\section{PEP 227: Nested Scopes}
961
962In Python 2.1, statically nested scopes were added as an optional
963feature, to be enabled by a \code{from __future__ import
964nested_scopes} directive. In 2.2 nested scopes no longer need to be
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000965specially enabled, and are now always present. The rest of this section
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000966is a copy of the description of nested scopes from my ``What's New in
967Python 2.1'' document; if you read it when 2.1 came out, you can skip
968the rest of this section.
969
970The largest change introduced in Python 2.1, and made complete in 2.2,
971is to Python's scoping rules. In Python 2.0, at any given time there
972are at most three namespaces used to look up variable names: local,
973module-level, and the built-in namespace. This often surprised people
974because it didn't match their intuitive expectations. For example, a
975nested recursive function definition doesn't work:
976
977\begin{verbatim}
978def f():
979 ...
980 def g(value):
981 ...
982 return g(value-1) + 1
983 ...
984\end{verbatim}
985
986The function \function{g()} will always raise a \exception{NameError}
987exception, because the binding of the name \samp{g} isn't in either
988its local namespace or in the module-level namespace. This isn't much
989of a problem in practice (how often do you recursively define interior
990functions like this?), but this also made using the \keyword{lambda}
991statement clumsier, and this was a problem in practice. In code which
992uses \keyword{lambda} you can often find local variables being copied
993by passing them as the default values of arguments.
994
995\begin{verbatim}
996def find(self, name):
997 "Return list of any entries equal to 'name'"
998 L = filter(lambda x, name=name: x == name,
999 self.list_attribute)
1000 return L
1001\end{verbatim}
1002
1003The readability of Python code written in a strongly functional style
1004suffers greatly as a result.
1005
1006The most significant change to Python 2.2 is that static scoping has
1007been added to the language to fix this problem. As a first effect,
1008the \code{name=name} default argument is now unnecessary in the above
1009example. Put simply, when a given variable name is not assigned a
1010value within a function (by an assignment, or the \keyword{def},
1011\keyword{class}, or \keyword{import} statements), references to the
1012variable will be looked up in the local namespace of the enclosing
1013scope. A more detailed explanation of the rules, and a dissection of
1014the implementation, can be found in the PEP.
1015
1016This change may cause some compatibility problems for code where the
1017same variable name is used both at the module level and as a local
1018variable within a function that contains further function definitions.
1019This seems rather unlikely though, since such code would have been
1020pretty confusing to read in the first place.
1021
1022One side effect of the change is that the \code{from \var{module}
1023import *} and \keyword{exec} statements have been made illegal inside
1024a function scope under certain conditions. The Python reference
1025manual has said all along that \code{from \var{module} import *} is
1026only legal at the top level of a module, but the CPython interpreter
1027has never enforced this before. As part of the implementation of
1028nested scopes, the compiler which turns Python source into bytecodes
1029has to generate different code to access variables in a containing
1030scope. \code{from \var{module} import *} and \keyword{exec} make it
1031impossible for the compiler to figure this out, because they add names
1032to the local namespace that are unknowable at compile time.
1033Therefore, if a function contains function definitions or
1034\keyword{lambda} expressions with free variables, the compiler will
1035flag this by raising a \exception{SyntaxError} exception.
1036
1037To make the preceding explanation a bit clearer, here's an example:
1038
1039\begin{verbatim}
1040x = 1
1041def f():
1042 # The next line is a syntax error
1043 exec 'x=2'
1044 def g():
1045 return x
1046\end{verbatim}
1047
1048Line 4 containing the \keyword{exec} statement is a syntax error,
1049since \keyword{exec} would define a new local variable named \samp{x}
1050whose value should be accessed by \function{g()}.
1051
1052This shouldn't be much of a limitation, since \keyword{exec} is rarely
1053used in most Python code (and when it is used, it's often a sign of a
1054poor design anyway).
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +00001055
1056\begin{seealso}
1057
1058\seepep{227}{Statically Nested Scopes}{Written and implemented by
1059Jeremy Hylton.}
1060
1061\end{seealso}
1062
Andrew M. Kuchlinga43e7032001-06-27 20:32:12 +00001063
1064%======================================================================
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +00001065\section{New and Improved Modules}
1066
1067\begin{itemize}
1068
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +00001069 \item The \module{xmlrpclib} module was contributed to the standard
Andrew M. Kuchling9da3efd2002-04-01 19:22:34 +00001070 library by Fredrik Lundh, providing support for writing XML-RPC
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +00001071 clients. XML-RPC is a simple remote procedure call protocol built on
Andrew M. Kuchling8c69c91b2001-08-07 14:28:58 +00001072 top of HTTP and XML. For example, the following snippet retrieves a
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +00001073 list of RSS channels from the O'Reilly Network, and then
1074 lists the recent headlines for one channel:
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +00001075
1076\begin{verbatim}
1077import xmlrpclib
1078s = xmlrpclib.Server(
1079 'http://www.oreillynet.com/meerkat/xml-rpc/server.php')
1080channels = s.meerkat.getChannels()
1081# channels is a list of dictionaries, like this:
1082# [{'id': 4, 'title': 'Freshmeat Daily News'}
1083# {'id': 190, 'title': '32Bits Online'},
1084# {'id': 4549, 'title': '3DGamers'}, ... ]
1085
1086# Get the items for one channel
1087items = s.meerkat.getItems( {'channel': 4} )
1088
1089# 'items' is another list of dictionaries, like this:
1090# [{'link': 'http://freshmeat.net/releases/52719/',
1091# 'description': 'A utility which converts HTML to XSL FO.',
1092# 'title': 'html2fo 0.3 (Default)'}, ... ]
1093\end{verbatim}
1094
Andrew M. Kuchlingd4707e32001-09-28 20:46:46 +00001095The \module{SimpleXMLRPCServer} module makes it easy to create
1096straightforward XML-RPC servers. See \url{http://www.xmlrpc.com/} for
1097more information about XML-RPC.
1098
Andrew M. Kuchling433b5c42001-10-30 21:36:04 +00001099 \item The new \module{hmac} module implements the HMAC
Andrew M. Kuchlingd4707e32001-09-28 20:46:46 +00001100 algorithm described by \rfc{2104}.
Andrew M. Kuchling433b5c42001-10-30 21:36:04 +00001101 (Contributed by Gerhard H\"aring.)
1102
1103 \item Several functions that originally returned lengthy tuples now
1104 return pseudo-sequences that still behave like tuples but also have
1105 mnemonic attributes such as member{st_mtime} or \member{tm_year}.
1106 The enhanced functions include \function{stat()},
1107 \function{fstat()}, \function{statvfs()}, and \function{fstatvfs()}
1108 in the \module{os} module, and \function{localtime()},
1109 \function{gmtime()}, and \function{strptime()} in the \module{time}
1110 module.
1111
1112 For example, to obtain a file's size using the old tuples, you'd end
1113 up writing something like \code{file_size =
1114 os.stat(filename)[stat.ST_SIZE]}, but now this can be written more
1115 clearly as \code{file_size = os.stat(filename).st_size}.
1116
1117 The original patch for this feature was contributed by Nick Mathewson.
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +00001118
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +00001119 \item The Python profiler has been extensively reworked and various
Andrew M. Kuchling9da3efd2002-04-01 19:22:34 +00001120 errors in its output have been corrected. (Contributed by
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +00001121 Fred~L. Drake, Jr. and Tim Peters.)
1122
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +00001123 \item The \module{socket} module can be compiled to support IPv6;
Andrew M. Kuchlingddeb1352001-07-16 14:35:52 +00001124 specify the \longprogramopt{enable-ipv6} option to Python's configure
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +00001125 script. (Contributed by Jun-ichiro ``itojun'' Hagino.)
1126
1127 \item Two new format characters were added to the \module{struct}
1128 module for 64-bit integers on platforms that support the C
1129 \ctype{long long} type. \samp{q} is for a signed 64-bit integer,
1130 and \samp{Q} is for an unsigned one. The value is returned in
1131 Python's long integer type. (Contributed by Tim Peters.)
1132
1133 \item In the interpreter's interactive mode, there's a new built-in
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +00001134 function \function{help()} that uses the \module{pydoc} module
1135 introduced in Python 2.1 to provide interactive help.
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +00001136 \code{help(\var{object})} displays any available help text about
Fred Drakea030c762002-05-02 17:55:26 +00001137 \var{object}. \function{help()} with no argument puts you in an online
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +00001138 help utility, where you can enter the names of functions, classes,
1139 or modules to read their help text.
1140 (Contributed by Guido van Rossum, using Ka-Ping Yee's \module{pydoc} module.)
1141
1142 \item Various bugfixes and performance improvements have been made
Andrew M. Kuchling4cf52a92001-07-17 12:48:48 +00001143 to the SRE engine underlying the \module{re} module. For example,
Andrew M. Kuchlingbeb38552001-10-22 14:11:06 +00001144 the \function{re.sub()} and \function{re.split()} functions have
1145 been rewritten in C. Another contributed patch speeds up certain
Andrew M. Kuchling433b5c42001-10-30 21:36:04 +00001146 Unicode character ranges by a factor of two, and a new \method{finditer()}
1147 method that returns an iterator over all the non-overlapping matches in
1148 a given string.
1149 (SRE is maintained by
Andrew M. Kuchlingbeb38552001-10-22 14:11:06 +00001150 Fredrik Lundh. The BIGCHARSET patch was contributed by Martin von
1151 L\"owis.)
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +00001152
Andrew M. Kuchling1efd7ad2001-09-14 16:19:27 +00001153 \item The \module{smtplib} module now supports \rfc{2487}, ``Secure
1154 SMTP over TLS'', so it's now possible to encrypt the SMTP traffic
1155 between a Python program and the mail transport agent being handed a
Andrew M. Kuchling433b5c42001-10-30 21:36:04 +00001156 message. \module{smtplib} also supports SMTP authentication.
1157 (Contributed by Gerhard H\"aring.)
Andrew M. Kuchling1efd7ad2001-09-14 16:19:27 +00001158
Andrew M. Kuchlinga6d2a042001-07-20 18:34:34 +00001159 \item The \module{imaplib} module, maintained by Piers Lauder, has
1160 support for several new extensions: the NAMESPACE extension defined
1161 in \rfc{2342}, SORT, GETACL and SETACL. (Contributed by Anthony
1162 Baxter and Michel Pelletier.)
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +00001163
Andrew M. Kuchlingd4707e32001-09-28 20:46:46 +00001164 \item The \module{rfc822} module's parsing of email addresses is now
1165 compliant with \rfc{2822}, an update to \rfc{822}. (The module's
1166 name is \emph{not} going to be changed to \samp{rfc2822}.) A new
1167 package, \module{email}, has also been added for parsing and
1168 generating e-mail messages. (Contributed by Barry Warsaw, and
1169 arising out of his work on Mailman.)
Andrew M. Kuchling77707672001-07-31 15:51:16 +00001170
Andrew M. Kuchlingbbde5882001-10-31 13:13:36 +00001171 \item The \module{difflib} module now contains a new \class{Differ}
1172 class for producing human-readable lists of changes (a ``delta'')
1173 between two sequences of lines of text. There are also two
1174 generator functions, \function{ndiff()} and \function{restore()},
1175 which respectively return a delta from two sequences, or one of the
1176 original sequences from a delta. (Grunt work contributed by David
1177 Goodger, from ndiff.py code by Tim Peters who then did the
1178 generatorization.)
1179
Andrew M. Kuchling77707672001-07-31 15:51:16 +00001180 \item New constants \constant{ascii_letters},
1181 \constant{ascii_lowercase}, and \constant{ascii_uppercase} were
1182 added to the \module{string} module. There were several modules in
1183 the standard library that used \constant{string.letters} to mean the
1184 ranges A-Za-z, but that assumption is incorrect when locales are in
1185 use, because \constant{string.letters} varies depending on the set
1186 of legal characters defined by the current locale. The buggy
1187 modules have all been fixed to use \constant{ascii_letters} instead.
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +00001188 (Reported by an unknown person; fixed by Fred~L. Drake, Jr.)
Andrew M. Kuchling77707672001-07-31 15:51:16 +00001189
Andrew M. Kuchling8c69c91b2001-08-07 14:28:58 +00001190 \item The \module{mimetypes} module now makes it easier to use
1191 alternative MIME-type databases by the addition of a
1192 \class{MimeTypes} class, which takes a list of filenames to be
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +00001193 parsed. (Contributed by Fred~L. Drake, Jr.)
Andrew M. Kuchling8c69c91b2001-08-07 14:28:58 +00001194
Andrew M. Kuchlingd6e40e22001-09-10 16:18:50 +00001195 \item A \class{Timer} class was added to the \module{threading}
1196 module that allows scheduling an activity to happen at some future
1197 time. (Contributed by Itamar Shtull-Trauring.)
Andrew M. Kuchling2f0047a2001-09-05 14:53:31 +00001198
Andrew M. Kuchling77707672001-07-31 15:51:16 +00001199\end{itemize}
1200
1201
1202%======================================================================
1203\section{Interpreter Changes and Fixes}
1204
1205Some of the changes only affect people who deal with the Python
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +00001206interpreter at the C level because they're writing Python extension modules,
Andrew M. Kuchling77707672001-07-31 15:51:16 +00001207embedding the interpreter, or just hacking on the interpreter itself.
1208If you only write Python code, none of the changes described here will
1209affect you very much.
1210
1211\begin{itemize}
1212
1213 \item Profiling and tracing functions can now be implemented in C,
1214 which can operate at much higher speeds than Python-based functions
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +00001215 and should reduce the overhead of profiling and tracing. This
1216 will be of interest to authors of development environments for
Andrew M. Kuchling77707672001-07-31 15:51:16 +00001217 Python. Two new C functions were added to Python's API,
1218 \cfunction{PyEval_SetProfile()} and \cfunction{PyEval_SetTrace()}.
1219 The existing \function{sys.setprofile()} and
1220 \function{sys.settrace()} functions still exist, and have simply
1221 been changed to use the new C-level interface. (Contributed by Fred
1222 L. Drake, Jr.)
1223
1224 \item Another low-level API, primarily of interest to implementors
1225 of Python debuggers and development tools, was added.
1226 \cfunction{PyInterpreterState_Head()} and
1227 \cfunction{PyInterpreterState_Next()} let a caller walk through all
1228 the existing interpreter objects;
1229 \cfunction{PyInterpreterState_ThreadHead()} and
1230 \cfunction{PyThreadState_Next()} allow looping over all the thread
1231 states for a given interpreter. (Contributed by David Beazley.)
1232
1233 \item A new \samp{et} format sequence was added to
1234 \cfunction{PyArg_ParseTuple}; \samp{et} takes both a parameter and
1235 an encoding name, and converts the parameter to the given encoding
1236 if the parameter turns out to be a Unicode string, or leaves it
1237 alone if it's an 8-bit string, assuming it to already be in the
1238 desired encoding. This differs from the \samp{es} format character,
1239 which assumes that 8-bit strings are in Python's default ASCII
1240 encoding and converts them to the specified new encoding.
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +00001241 (Contributed by M.-A. Lemburg, and used for the MBCS support on
1242 Windows described in the following section.)
Andrew M. Kuchlingcf31d5d2001-10-26 20:37:55 +00001243
1244 \item A different argument parsing function,
1245 \cfunction{PyArg_UnpackTuple()}, has been added that's simpler and
1246 presumably faster. Instead of specifying a format string, the
1247 caller simply gives the minimum and maximum number of arguments
Fred Drakea030c762002-05-02 17:55:26 +00001248 expected, and a set of pointers to \ctype{PyObject*} variables that
Andrew M. Kuchlingcf31d5d2001-10-26 20:37:55 +00001249 will be filled in with argument values.
1250
Andrew M. Kuchling0ab31b82001-08-29 01:16:54 +00001251 \item Two new flags \constant{METH_NOARGS} and \constant{METH_O} are
1252 available in method definition tables to simplify implementation of
1253 methods with no arguments or a single untyped argument. Calling
1254 such methods is more efficient than calling a corresponding method
1255 that uses \constant{METH_VARARGS}.
1256 Also, the old \constant{METH_OLDARGS} style of writing C methods is
1257 now officially deprecated.
1258
1259\item
1260 Two new wrapper functions, \cfunction{PyOS_snprintf()} and
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +00001261 \cfunction{PyOS_vsnprintf()} were added to provide
Andrew M. Kuchling0ab31b82001-08-29 01:16:54 +00001262 cross-platform implementations for the relatively new
1263 \cfunction{snprintf()} and \cfunction{vsnprintf()} C lib APIs. In
1264 contrast to the standard \cfunction{sprintf()} and
1265 \cfunction{vsprintf()} functions, the Python versions check the
1266 bounds of the buffer used to protect against buffer overruns.
1267 (Contributed by M.-A. Lemburg.)
Andrew M. Kuchling77707672001-07-31 15:51:16 +00001268
Andrew M. Kuchlingccf04652001-11-26 18:15:44 +00001269 \item The \cfunction{_PyTuple_Resize()} function has lost an unused
1270 parameter, so now it takes 2 parameters instead of 3. The third
1271 argument was never used, and can simply be discarded when porting
1272 code from earlier versions to Python 2.2.
1273
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +00001274\end{itemize}
1275
1276
1277%======================================================================
1278\section{Other Changes and Fixes}
1279
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +00001280As usual there were a bunch of other improvements and bugfixes
1281scattered throughout the source tree. A search through the CVS change
Andrew M. Kuchling9da3efd2002-04-01 19:22:34 +00001282logs finds there were 527 patches applied and 683 bugs fixed between
Andrew M. Kuchlingcab94a12002-11-12 18:59:20 +00001283Python 2.1 and 2.2; 2.2.1 applied 139 patches and fixed 143 bugs;
12842.2.2 applied 106 patches and fixed 82 bugs. These figures are likely
1285to be underestimates.
Andrew M. Kuchling9da3efd2002-04-01 19:22:34 +00001286
1287Some of the more notable changes are:
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +00001288
1289\begin{itemize}
1290
Andrew M. Kuchling0e03f582001-08-30 21:30:16 +00001291 \item The code for the MacOS port for Python, maintained by Jack
1292 Jansen, is now kept in the main Python CVS tree, and many changes
Andrew M. Kuchling279e7442001-10-22 02:03:40 +00001293 have been made to support MacOS~X.
Andrew M. Kuchling0e03f582001-08-30 21:30:16 +00001294
1295The most significant change is the ability to build Python as a
1296framework, enabled by supplying the \longprogramopt{enable-framework}
1297option to the configure script when compiling Python. According to
1298Jack Jansen, ``This installs a self-contained Python installation plus
Andrew M. Kuchling279e7442001-10-22 02:03:40 +00001299the OS~X framework "glue" into
Andrew M. Kuchling0e03f582001-08-30 21:30:16 +00001300\file{/Library/Frameworks/Python.framework} (or another location of
1301choice). For now there is little immediate added benefit to this
1302(actually, there is the disadvantage that you have to change your PATH
1303to be able to find Python), but it is the basis for creating a
1304full-blown Python application, porting the MacPython IDE, possibly
1305using Python as a standard OSA scripting language and much more.''
1306
1307Most of the MacPython toolbox modules, which interface to MacOS APIs
Andrew M. Kuchling279e7442001-10-22 02:03:40 +00001308such as windowing, QuickTime, scripting, etc. have been ported to OS~X,
Andrew M. Kuchlingbeb38552001-10-22 14:11:06 +00001309but they've been left commented out in \file{setup.py}. People who want
Andrew M. Kuchling0e03f582001-08-30 21:30:16 +00001310to experiment with these modules can uncomment them manually.
1311
1312% Jack's original comments:
1313%The main change is the possibility to build Python as a
1314%framework. This installs a self-contained Python installation plus the
1315%OSX framework "glue" into /Library/Frameworks/Python.framework (or
1316%another location of choice). For now there is little immedeate added
1317%benefit to this (actually, there is the disadvantage that you have to
1318%change your PATH to be able to find Python), but it is the basis for
1319%creating a fullblown Python application, porting the MacPython IDE,
1320%possibly using Python as a standard OSA scripting language and much
1321%more. You enable this with "configure --enable-framework".
1322
1323%The other change is that most MacPython toolbox modules, which
1324%interface to all the MacOS APIs such as windowing, quicktime,
1325%scripting, etc. have been ported. Again, most of these are not of
1326%immedeate use, as they need a full application to be really useful, so
1327%they have been commented out in setup.py. People wanting to experiment
1328%can uncomment them. Gestalt and Internet Config modules are enabled by
1329%default.
Andrew M. Kuchling0e03f582001-08-30 21:30:16 +00001330
Andrew M. Kuchling2cd712b2001-07-16 13:39:08 +00001331 \item Keyword arguments passed to builtin functions that don't take them
1332 now cause a \exception{TypeError} exception to be raised, with the
1333 message "\var{function} takes no keyword arguments".
1334
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +00001335 \item Weak references, added in Python 2.1 as an extension module,
1336 are now part of the core because they're used in the implementation
1337 of new-style classes. The \exception{ReferenceError} exception has
1338 therefore moved from the \module{weakref} module to become a
1339 built-in exception.
1340
Andrew M. Kuchling94a7eba2001-08-15 15:55:48 +00001341 \item A new script, \file{Tools/scripts/cleanfuture.py} by Tim
1342 Peters, automatically removes obsolete \code{__future__} statements
1343 from Python source code.
Andrew M. Kuchling2cd712b2001-07-16 13:39:08 +00001344
Andrew M. Kuchling4f9e2202001-10-29 18:09:42 +00001345 \item An additional \var{flags} argument has been added to the
1346 built-in function \function{compile()}, so the behaviour of
1347 \code{__future__} statements can now be correctly observed in
1348 simulated shells, such as those presented by IDLE and other
1349 development environments. This is described in \pep{264}.
1350 (Contributed by Michael Hudson.)
1351
Andrew M. Kuchling2cd712b2001-07-16 13:39:08 +00001352 \item The new license introduced with Python 1.6 wasn't
1353 GPL-compatible. This is fixed by some minor textual changes to the
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +00001354 2.2 license, so it's now legal to embed Python inside a GPLed
1355 program again. Note that Python itself is not GPLed, but instead is
1356 under a license that's essentially equivalent to the BSD license,
1357 same as it always was. The license changes were also applied to the
1358 Python 2.0.1 and 2.1.1 releases.
Andrew M. Kuchling2cd712b2001-07-16 13:39:08 +00001359
Andrew M. Kuchlingf4ccf582001-07-31 01:11:36 +00001360 \item When presented with a Unicode filename on Windows, Python will
1361 now convert it to an MBCS encoded string, as used by the Microsoft
1362 file APIs. As MBCS is explicitly used by the file APIs, Python's
1363 choice of ASCII as the default encoding turns out to be an
Fred Drakea030c762002-05-02 17:55:26 +00001364 annoyance. On \UNIX, the locale's character set is used if
Andrew M. Kuchling433b5c42001-10-30 21:36:04 +00001365 \function{locale.nl_langinfo(CODESET)} is available. (Windows
1366 support was contributed by Mark Hammond with assistance from
Fred Drakea030c762002-05-02 17:55:26 +00001367 Marc-Andr\'e Lemburg. \UNIX{} support was added by Martin von L\"owis.)
Andrew M. Kuchling8cfa9052001-07-19 01:19:59 +00001368
Andrew M. Kuchlingd6e40e22001-09-10 16:18:50 +00001369 \item Large file support is now enabled on Windows. (Contributed by
1370 Tim Peters.)
1371
Andrew M. Kuchling2cd712b2001-07-16 13:39:08 +00001372 \item The \file{Tools/scripts/ftpmirror.py} script
1373 now parses a \file{.netrc} file, if you have one.
Andrew M. Kuchling4cf52a92001-07-17 12:48:48 +00001374 (Contributed by Mike Romberg.)
Andrew M. Kuchling2cd712b2001-07-16 13:39:08 +00001375
Andrew M. Kuchling4cf52a92001-07-17 12:48:48 +00001376 \item Some features of the object returned by the
1377 \function{xrange()} function are now deprecated, and trigger
1378 warnings when they're accessed; they'll disappear in Python 2.3.
1379 \class{xrange} objects tried to pretend they were full sequence
1380 types by supporting slicing, sequence multiplication, and the
1381 \keyword{in} operator, but these features were rarely used and
1382 therefore buggy. The \method{tolist()} method and the
1383 \member{start}, \member{stop}, and \member{step} attributes are also
1384 being deprecated. At the C level, the fourth argument to the
1385 \cfunction{PyRange_New()} function, \samp{repeat}, has also been
1386 deprecated.
1387
Andrew M. Kuchling8cfa9052001-07-19 01:19:59 +00001388 \item There were a bunch of patches to the dictionary
1389 implementation, mostly to fix potential core dumps if a dictionary
1390 contains objects that sneakily changed their hash value, or mutated
1391 the dictionary they were contained in. For a while python-dev fell
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +00001392 into a gentle rhythm of Michael Hudson finding a case that dumped
1393 core, Tim Peters fixing the bug, Michael finding another case, and round
Andrew M. Kuchling8cfa9052001-07-19 01:19:59 +00001394 and round it went.
1395
Andrew M. Kuchling33a3b632001-09-04 21:25:58 +00001396 \item On Windows, Python can now be compiled with Borland C thanks
1397 to a number of patches contributed by Stephen Hansen, though the
1398 result isn't fully functional yet. (But this \emph{is} progress...)
Andrew M. Kuchling8c69c91b2001-08-07 14:28:58 +00001399
Andrew M. Kuchlingf4ccf582001-07-31 01:11:36 +00001400 \item Another Windows enhancement: Wise Solutions generously offered
1401 PythonLabs use of their InstallerMaster 8.1 system. Earlier
1402 PythonLabs Windows installers used Wise 5.0a, which was beginning to
1403 show its age. (Packaged up by Tim Peters.)
1404
Andrew M. Kuchling8c69c91b2001-08-07 14:28:58 +00001405 \item Files ending in \samp{.pyw} can now be imported on Windows.
1406 \samp{.pyw} is a Windows-only thing, used to indicate that a script
1407 needs to be run using PYTHONW.EXE instead of PYTHON.EXE in order to
1408 prevent a DOS console from popping up to display the output. This
1409 patch makes it possible to import such scripts, in case they're also
1410 usable as modules. (Implemented by David Bolen.)
1411
Andrew M. Kuchling8cfa9052001-07-19 01:19:59 +00001412 \item On platforms where Python uses the C \cfunction{dlopen()} function
1413 to load extension modules, it's now possible to set the flags used
1414 by \cfunction{dlopen()} using the \function{sys.getdlopenflags()} and
1415 \function{sys.setdlopenflags()} functions. (Contributed by Bram Stolk.)
Andrew M. Kuchling2f0047a2001-09-05 14:53:31 +00001416
Andrew M. Kuchling26c39bf2001-09-10 03:20:53 +00001417 \item The \function{pow()} built-in function no longer supports 3
1418 arguments when floating-point numbers are supplied.
Andrew M. Kuchling1497b622001-09-24 14:51:16 +00001419 \code{pow(\var{x}, \var{y}, \var{z})} returns \code{(x**y) \% z}, but
Andrew M. Kuchling26c39bf2001-09-10 03:20:53 +00001420 this is never useful for floating point numbers, and the final
1421 result varies unpredictably depending on the platform. A call such
Andrew M. Kuchlingd6e40e22001-09-10 16:18:50 +00001422 as \code{pow(2.0, 8.0, 7.0)} will now raise a \exception{TypeError}
Andrew M. Kuchling26c39bf2001-09-10 03:20:53 +00001423 exception.
Andrew M. Kuchling77707672001-07-31 15:51:16 +00001424
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +00001425\end{itemize}
1426
1427
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +00001428%======================================================================
1429\section{Acknowledgements}
1430
1431The author would like to thank the following people for offering
Andrew M. Kuchlingb83769c2001-10-26 20:07:03 +00001432suggestions, corrections and assistance with various drafts of this
1433article: Fred Bremmer, Keith Briggs, Andrew Dalke, Fred~L. Drake, Jr.,
Andrew M. Kuchlingccf04652001-11-26 18:15:44 +00001434Carel Fellinger, David Goodger, Mark Hammond, Stephen Hansen, Michael
1435Hudson, Jack Jansen, Marc-Andr\'e Lemburg, Martin von L\"owis, Fredrik
1436Lundh, Michael McLay, Nick Mathewson, Paul Moore, Gustavo Niemeyer,
Andrew M. Kuchling17850f72002-04-10 21:53:22 +00001437Don O'Donnell, Joonas Paalasma, Tim Peters, Jens Quade, Tom Reinhardt, Neil
Andrew M. Kuchling71dd7902002-11-12 18:45:46 +00001438Schemenauer, Guido van Rossum, Greg Ward, Edward Welbourne.
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +00001439
1440\end{document}