How about adding rational fraction to Python?

Paul Rubin http
Wed Feb 27 21:43:24 EST 2008


Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> writes:
> def pmean(data):  # Paul Rubin's mean
>     """Returns the arithmetic mean of data, unless data is all 
>     ints, in which case returns the mean rounded to the nearest 
>     integer less than the arithmetic mean."""
>     s = sum(data)
>     if isinstance(s, int): return s//len(data)
>     else: return s/len(data)

Scheme and Common Lisp do automatic conversion and they thought out
the semantics rather carefully, and I think both of them return
exact rationals in this situation (int/int division).  I agree
with you that using // as above is pretty weird and it may be
preferable to raise TypeError on any use of int/int (require
either an explicit conversion, or use of //).



More information about the Python-list mailing list