Why Python 3?

Gregory Ewing greg.ewing at canterbury.ac.nz
Sun Apr 20 19:56:43 EDT 2014


Ian Kelly wrote:

> def average(values):
>     return sum(values) / len(values)
> 
> This works for decimals, it works for fractions, it works for complex 
> numbers, it works for numpy types, and in Python 3 it works for ints.

That depends on what you mean by "works". I would actually
find it rather disturbing if an average() function implicitly
used floor division when given all ints.

The reason is that people often use ints as stand-ins for
floats in computations that are conceptually non-integer.
So a general-purpose function like average(), given a list
of ints, has no way of knowing whether they're intended
to be interpreted as ints or floats.

To my way of thinking, floor division is a specialised
operation that is only wanted in particular circumstances.
It's rare that I would actually want it done in the
context of taking an average, and if I do, I would rather
be explicit about it using e.g. int(floor(average(...))
or a specialised int_average() function.

-- 
Greg



More information about the Python-list mailing list