Division considered un-Pythonic (Re: Case-sensitivity: why -- or why not? (was Re: Damnation!))

Greg Ewing greg at cosc.canterbury.ac.nz
Tue May 30 04:26:27 CEST 2000


On Sun, 28 May 2000 20:14:46 +0200, Thomas Malik wrote:

> And then, i mean, once told, one shouldn't forget that 1/2 == 0. A person
> forgetting that all the time should propably learn something else.

You're failing to take the dynamicness of Python into account.
The problem isn't with writing 1/2 and forgetting what it means; the
problem is with writing a/b when you can't be sure until run time
what type a and b are.

Most of the time in Python it doesn't matter if you leave a number
as an integer until such time as it needs to become a float, whereupon
conversion is automatic. This is very convenient -- until you want
to do real division, at which point it all falls apart. Either you
explicitly convert to floats at every division, which clutters up
the code, or you have to take care to convert all numbers which
you might conceivably want to divide to floats on input. Either
approach is error prone in the worst possible way, i.e. mistakes
lead to bugs which are hard to trace.

I assume that no-one would disagree with the following statements:

1. Integer division and real (or rational, if you prefer) division
   are different operations.

2. Whenever you write a division operation into your code, you
   always know which one you want.

Currently, Python provides no way for you to directly specify
which one you want -- instead, it uses a heuristic based on the
run-time types of the arguments. This clearly violates the principle
of not trying to guess what the programmer meant. Therefore, it
is un-Pythonic. QED.

-- 
Greg Ewing, Computer Science Dept,
+--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg at cosc.canterbury.ac.nz	   +--------------------------------------+



More information about the Python-list mailing list