Fractions as result from divisions (was: Re: tough-to-explain Python)

Ulrich Eckhardt eckhardt at satorlaser.com
Wed Jul 8 11:24:28 CEST 2009


Bearophile wrote:
> For example a novice wants to see 124 / 38 to return the 62/19
> fraction and not 3 or 3.263157894736842 :-)

Python has adopted the latter of the three for operator / and the the second
one for operator //. I wonder if it was considered to just return a
fraction from that operation.

 x = 3/5
 -> x = fraction(3, 5)
 x = x*7
 -> x = fraction(21, 5)
 x = x/2
 -> x = fraction(21, 10)
 range(x)
 -> error, x not an integer
 range(int(x))
 -> [0, 1,]
 y = float(x)
 -> y = 2.1

This would have allowed keeping the operator what people are used to. On the
other hand, implicit conversion to either of float or int would have been
avoided, which is usually the #1 cause for subtle problems.


Uli

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932




More information about the Python-list mailing list