[Tutor] Fwd: Re: fractions from Fractions

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Tue Feb 6 10:00:38 EST 2018


On 06.02.2018 10:53, Alan Gauld via Tutor wrote:
> 
> You would have needed something like
> 
> fraction = Fraction(input('>'))
> 
> Assuming Fraction has a string based constructor which
> I don't think it does. I really don't think Fraction
> helps you here.

Oh, most certainly it has - though it may accept more forms of input 
than the OP needs/wants:

 >>> from fractions import Fraction
 >>> Fraction('2/3')
Fraction(2, 3)
 >>> Fraction('2.3')
Fraction(23, 10)
 >>> Fraction('2e-3')
Fraction(1, 500)
 >>>

Since fractions is pure Python it may also be instructive to study its 
source:
https://github.com/python/cpython/blob/master/Lib/fractions.py



More information about the Tutor mailing list