Comment on PEP-0238
Terry Reedy
tjreedy at home.com
Thu Jul 12 00:49:07 EDT 2001
> By the way (this is for Terry :-), int/int in Python does not do the
> same as it does in C for negative operands.
>
> --Guido van Rossum (home page: http://www.python.org/~guido/)
I may have noticed this when I switched several years ago, but indeed had
forgotten since. Fortunately, I know enough not to rely on my memory for
something I would only do occasionally.
---
Tuesday afternoon's experiment: I asked my (non-programmer,
non-mathematical) wife, "What is eight divided by three." Even with a hint
("eight, not eight point zero"), she replied 'two point something or
other." So Python's current behavior would have initially urprised her.
On the other hand, when I said to think back to second grade, she easily
remembered quotient-remainder answers to division.problems. After I
explained Python's notation, , she thought she could handle using '.0' or
not to indicate which type of answer she wanted. It was unsurprising to
her that she should have a choice. This took at most two minutes.
---
That said, I can imagine that it would have been better to have defined
Python's arithmetic a little differently back at the beginning. If I had a
time machine, here is what I would propose: use / for fractional division
and use div, mod, and divmod (as infix operators) for whole number,
remainder division. A simple pair of rules that I think should be easy to
teach, learn, and remember.
Note 1: The omission of '.0' and 'float()' where now needed would more than
make up for the two extra chars in 'div' versus '/' and 'mod' versus '%'.
Note 2: 'mod' is the standard mathematical notation for the modulus
(remainer) operation. C used '%' because all its operators are symbolic.
This is not true for Python so there is no need to copy C (or any other
language that uses '5'), especially when paired with 'div'.
What would I propose now? If and when int division is changed (preferably
as a package of arithmetic changes -- see my response to Guido in Re:
Language change and code breaks), leave divmod a function if you want,
change / to div, and also change % to mod.
A) The three names obviously fit together very nicely, which the pair div,
% strikes me as a bit ugly. B) The overloading of (format) % with the
unrelated or ever conflicting (mod) % (one adds, the other tosses away)
strikes me as a slight wart on the language. Do beginners ever find this
confusing? And yes, do add corresponding magic methods to classes.
Terry J. Reedy
More information about the Python-list
mailing list