The most controversial change in Python 2.2 heralds the start of an effort
to fix an old design flaw that's been in Python from the beginning.
Currently Python's division operator,
/, behaves like C's
division operator when presented with two integer arguments: it
returns an integer result that's truncated down when there would be
a fractional part. For example,
3/2 is 1, not 1.5, and
(-1)/2 is -1, not -0.5. This means that the results of divison
can vary unexpectedly depending on the type of the two operands and
because Python is dynamically typed, it can be difficult to determine
the possible types of the operands.
(The controversy is over whether this is really a design flaw, and whether it's worth breaking existing code to fix this. It's caused endless discussions on python-dev, and in July 2001 erupted into an storm of acidly sarcastic postings on comp.lang.python. I won't argue for either side here and will stick to describing what's implemented in 2.2. Read PEP 238 for a summary of arguments and counter-arguments.)
Because this change might break code, it's being introduced very gradually. Python 2.2 begins the transition, but the switch won't be complete until Python 3.0.
First, I'll borrow some terminology from PEP 238. ``True division'' is the
division that most non-programmers are familiar with: 3/2 is 1.5, 1/4
is 0.25, and so forth. ``Floor division'' is what Python's
operator currently does when given integer operands; the result is the
floor of the value returned by true division. ``Classic division'' is
the current mixed behaviour of
/; it returns the result of
floor division when the operands are integers, and returns the result
of true division when one of the operands is a floating-point number.
Here are the changes 2.2 introduces:
//, is the floor division operator. (Yes, we know it looks like C++'s comment symbol.)
//always performs floor division no matter what the types of its operands are, so
1 // 2is 0 and
1.0 // 2.0is also 0.0.
// is always available in Python 2.2; you don't need to enable
it using a
from __future__ import divisionin a module, the
/operator will be changed to return the result of true division, so
1/2is 0.5. Without the
/still means classic division. The default meaning of
/will not change until Python 3.0.
PyNumberMethodsstructure so extension types can define the two operators.
See About this document... for information on suggesting changes.