Future division patch available (PEP 238)
For those interested in the future of the division operator a la PEP 238, I've produced a reasonably complete patch (relative to the CVS trunk, but it probably also works for the descr-branch or the 2.2a1 release). Get it here: http://sourceforge.net/tracker/index.php?func=detail&aid=443474&group_id=5470&atid=305470 It works as follows: - unconditionally, there's a new operator // that will always do int division (and an in-place companion //=). - by default, / is unchanged (and so is /=). - after "from __future__ import division", / is changed to return a float result from int or long operands (and so is /=). Read the patch description for more details; the implementation of int and float division are semi-lame. There's no warning yet for int division returning a truncated result; I'm not sure if I want such a warning to be part of 2.2 (maybe if it's off by default). I'm cc'ing Bruce Sherwood and Davin Scherer, because they asked for this and used a similar implementation in VPython. When this patch (or something not entirely unlike it) is accepted into Python 2.2, they will no longer have to maintain their own hacked Python. (We've already added 10**-15 returning a float to 2.2a1, also specifically for them; that was easier because it used to be an error, so no backwards compatibility code or future statement is necessary there.) I thought again about the merits of the '//' operator vs. 'div' (either as a function or as a keyword binary operator), and figured that '//' is the best choice: it doesn't introduce a new keyword (which would cause more pain), and it works as an augmented assignment (//=) as well. --Guido van Rossum (home page: http://www.python.org/~guido/)
On Sun, 22 Jul 2001 00:36:38 -0400, Guido van Rossum
For those interested in the future of the division operator a la PEP 238, I've produced a reasonably complete patch (relative to the CVS trunk, but it probably also works for the descr-branch or the 2.2a1 release).
Do you want me to update PEP-0238 to reflect the new realities as to "open issues"? (I saw you already added a link to the patch in the PEP. Great!) -- gpg --keyserver keyserver.pgp.com --recv-keys 46D01BD6 54C4E1FE Secure (inaccessible): 4BD1 7705 EEC0 260A 7F21 4817 C7FC A636 46D0 1BD6 Insecure (accessible): C5A5 A8FA CA39 AB03 10B8 F116 1713 1BCF 54C4 E1FE
Guido,
I deeply respect your language design skills and I appreciate your
ongoing improvements. I'm impatiently looking forward to using many of
the features new in 2.2.
The future division patch is a different issue, though. While I agree
that the proposed changes are a definite improvement over the current
semantics, the issue of backwards compatibility is a huge problem
difficult to solve. I see the following problems:
- What's going to happen to code released into the wild (i.e., I can
change all my code but what about code I gave to others)?
- How can one write readable code working correctly in both old and
new Python versions?
- It takes a potentially huge effort to change all the existing code.
- In some cases, warnings might not be seen (e.g., they might land in
/dev/null or in some log files nobody looks at).
- If an application jumps versions (e.g., from 2.1 to 2.6), no warnings
might be generated at all.
- Upgrading to a new version of an application might break user scripts or
databases.
I have no idea how to tackle all these issues but I'll offer some
ideas nevertheless.
- If `int` always truncated (instead of truncating or rounding,
depending on how the C compiler does it), one could write reasonably
readable version independent code for truncating integer division.
Compare `int (a/b)` to `divmod (a, b) [0]` or
`int (math.floor (a/b))`.
- Just a wild idea: the problem you want to solve is that the existing
division operator mixes two totally different meanings and thus
leads to nasty surprises.
What if `/` applied to two integer values returned neither an
integer nor a float but an object carrying the float result but
behaving like an integer if used in an integer context?
For instance:
>>> x = 1/2
>>> type(x)
"Christian" == Christian Tanzer
writes:
[snipperoonio... lots of interesting stuff about real-life and seemingly highly dynamic Python deployments] Christian> To be honest, for TTTech design databases the change in Christian> division probably doesn't pose any problems. Due to Christian> user demand, the tools coerced divisions [in Christian> customer-written code] to floating point for a long Christian> time. "Due to customer demand"... well, seems to me you have given great support to PEP 238 with this. ;-) Bye, J PS: No, I'm not seeing you in the (raving) anti-PEP-238 camp, Christian. Your post was much too level-headed for this confusion to happen. ;-) -- Jürgen A. Erhard (juergen.erhard@gmx.net, jae@users.sourceforge.net) My WebHome: http://members.tripod.com/Juergen_Erhard "Those who would give up essential Liberty, to purchase a little temporary Safety, deserve neither Liberty nor Safety." -- B. Franklin
participants (4)
-
"Jürgen A. Erhard"
-
Guido van Rossum
-
Moshe Zadka
-
tanzer@swing.co.at