Comment on PEP-0238

Guido van Rossum guido at python.org
Sun Jul 8 08:21:28 EDT 2001


Timothy Grant <tjg at hyperlinq.net> writes:

> I'm not the most gifted programmer on the planet, but I have had no
> problem with the concept of integer division, in fact, when I need a
> calculator I usually open the Python interpreter and do my math on
> the command line. I've never had a problem remembering that if I
> want a float result I need to make one of the numbers a float.

Easy for you to say, since you seem to think mostly in terms of
integers.  For other people, and in other application domains, where
floating point is the only type that makes sense, it's very easy to
forget adding a decimal point to every constant you use.  A typical
example goes as follows:

    def velocity(distance, time):
        return distance / time

The author of this function clearly assumes that the arguments are
floats.  But a user, thinking that ints are automatically converted to
floats, it's totally necessary to write

    velocity(50000, 3600) # 50 km/h

and get a result that is plausible but incorrect.  Now imagine that
this is part of a much large program doing physical calculations.
Either we'd have to place a total ban on integer constants (a big
pain), or we'd have to place float() casts around the arguments of
function everwhere (an even bigger pain).

> # It looks ugly but it really isn't. A couple of really nice
> # Pythonisms
> # make it work right. The first is list insertion, and the
> # second is integer
> # division.
> 
> I guess I need to revise the comments in my code now...

The *concept* of integer division won't go away.  It will just be
spelled differently (as div(x,y) or x div y).  There's no need to
update your comment. :-)

--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-list mailing list