Division considered un-Pythonic (Re: Case-sensitivity: why -- or why not? (was Re: Damnation!))
olipt at mayo.edu
Tue May 30 16:19:24 CEST 2000
> 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.
Thank you for this clear explanation. I agree with this statement. I
have been bit several times by this problem of being able to specify which
kind of division I want and having it depend on input types.
More information about the Python-list