How about adding rational fraction to Python?

Paul Rubin http
Sun Mar 2 02:23:21 EST 2008


Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> writes:
> def mean(data): return sum(data)/len(data)
> 
> That does the right thing for data, no matter of what it consists of: 
> floats, ints, Decimals, rationals, complex numbers, or a mix of all of 
> the above.

One of those types is not like the others: for all of them except int,
the quotient operation actually is the inverse of multiplication.
So I'm unpersuaded that the "mean" operation above does the "right
thing" for ints.  If the integers being averaged were prices
in dollars, maybe the result type should even be decimal.

For this reason I think // is a good thing and I've gotten accustomed
to using it for integer division.  I can live with int/int=float but
find it sloppy and would be happier if int/int always threw an error
(convert explicitly if you want a particular type result).



More information about the Python-list mailing list