1212\section {Introduction }
1313
1414{\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 not up to
16- date at all. Please send any comments, bug reports, or questions, no
17- matter how minor, to
\email {
[email protected] }. }
15+ final version of Python 2.2 is released. Currently it's up to date
16+ for Python 2.2 alpha 1. Please send any comments, bug reports, or
17+ questions, no matter how minor, to
\email {
[email protected] }.
18+ }
1819
1920This article explains the new features in Python 2.2. Python 2.2
2021includes some significant changes that go far toward cleaning up the
@@ -135,9 +136,7 @@ \section{PEP 234: Iterators}
135136>>>
136137\end {verbatim }
137138
138- Iterator support has been added to some of Python's basic types. The
139- \keyword {in} operator now works on dictionaries, so \code {\var {key} in
140- dict} is now equivalent to \code {dict.has_key(\var {key})}.
139+ Iterator support has been added to some of Python's basic types.
141140Calling \function {iter()} on a dictionary will return an iterator
142141which loops over its keys:
143142
@@ -164,9 +163,13 @@ \section{PEP 234: Iterators}
164163That's just the default behaviour. If you want to iterate over keys,
165164values, or key/value pairs, you can explicitly call the
166165\method {iterkeys()}, \method {itervalues()}, or \method {iteritems()}
167- methods to get an appropriate iterator.
166+ methods to get an appropriate iterator. In a minor related change,
167+ the \keyword {in} operator now works on dictionaries, so
168+ \code {\var {key} in dict} is now equivalent to
169+ \code {dict.has_key(\var {key})}.
168170
169- Files also provide an iterator, which calls its \method {readline()}
171+
172+ Files also provide an iterator, which calls the \method {readline()}
170173method until there are no more lines in the file. This means you can
171174now read each line of a file using code like this:
172175
@@ -335,6 +338,76 @@ \section{PEP 255: Simple Generators}
335338\end {seealso }
336339
337340
341+ % ======================================================================
342+ \section {PEP 238: Changing the Division Operator }
343+
344+ The most controversial change in Python 2.2 is the start of an effort
345+ to fix an old design flaw that's been in Python from the beginning.
346+ Currently Python's division operator, \code {/}, behaves like C's
347+ division operator when presented with two integer arguments. It
348+ returns an integer result that's truncated down when there would be
349+ fractional part. For example, \code {3/2} is 1, not 1.5, and
350+ \code {(-1)/2} is -1, not -0.5. This means that the results of divison
351+ can vary unexpectedly depending on the type of the two operands and
352+ because Python is dynamically typed, it can be difficult to determine
353+ the possible types of the operands.
354+
355+ (The controversy is over whether this is \emph {really } a design flaw,
356+ and whether it's worth breaking existing code to fix this. It's
357+ caused endless discussions on python-dev and in July erupted into an
358+ storm of acidly sarcastic postings on \newsgroup {comp.lang.python}. I
359+ won't argue for either side here; read PEP 238 for a summary of
360+ arguments and counter-arguments.)
361+
362+ Because this change might break code, it's being introduced very
363+ gradually. Python 2.2 begins the transition, but the switch won't be
364+ complete until Python 3.0.
365+
366+ First, some terminology from PEP 238. `` True division'' is the
367+ division that most non-programmers are familiar with: 3/2 is 1.5, 1/4
368+ is 0.25, and so forth. `` Floor division'' is what Python's \code {/}
369+ operator currently does when given integer operands; the result is the
370+ floor of the value returned by true division. `` Classic division'' is
371+ the current mixed behaviour of \code {/}; it returns the result of
372+ floor division when the operands are integers, and returns the result
373+ of true division when one of the operands is a floating-point number.
374+
375+ Here are the changes 2.2 introduces:
376+
377+ \begin {itemize }
378+
379+ \item A new operator, \code {//}, is the floor division operator.
380+ (Yes, we know it looks like \Cpp 's comment symbol.) \code {//}
381+ \emph {always } returns the floor divison no matter what the types of
382+ its operands are, so \code {1 // 2} is 0 and \code {1.0 // 2.0} is also
383+ 0.0.
384+
385+ \code {//} is always available in Python 2.2; you don't need to enable
386+ it using a \code {__future__} statement.
387+
388+ \item By including a \code {from __future__ import true_division} in a
389+ module, the \code {/} operator will be changed to return the result of
390+ true division, so \code {1/2} is 0.5. Without the \code {__future__}
391+ statement, \code {/} still means classic division. The default meaning
392+ of \code {/} will not change until Python 3.0.
393+
394+ \item Classes can define methods called \method {__truediv__} and
395+ \method {__floordiv__} to overload the two division operators. At the
396+ C level, there are also slots in the \code {PyNumberMethods} structure
397+ so extension types can define the two operators.
398+
399+ % XXX a warning someday?
400+
401+ \end {itemize }
402+
403+ \begin {seealso }
404+
405+ \seepep {238}{Changing the Division Operator}{Written by Moshe Zadka and
406+ Guido van Rossum. Implemented by Guido van Rossum..}
407+
408+ \end {seealso }
409+
410+
338411% ======================================================================
339412\section {Unicode Changes }
340413
@@ -732,7 +805,8 @@ \section{Acknowledgements}
732805
733806The author would like to thank the following people for offering
734807suggestions and corrections to various drafts of this article: Fred
735- Bremmer, Keith Briggs, Fred L. Drake, Jr., Mark Hammond, Marc-Andr\' e
736- Lemburg, Tim Peters, Neil Schemenauer, Guido van Rossum.
808+ Bremmer, Keith Briggs, Fred L. Drake, Jr., Carel Fellinger, Mark
809+ Hammond, Marc-Andr\' e Lemburg, Tim Peters, Neil Schemenauer, Guido van
810+ Rossum.
737811
738812\end {document }
0 commit comments