Here's a patch that allows direct construction of a Fraction instance from a float or Decimal instance, performing an exact conversion in either case. >>> from fractions import Fraction >>> from decimal import Decimal >>> Fraction(1.1) Fraction(2476979795053773, 2251799813685248) >>> Fraction(Decimal('1.1')) Fraction(11, 10) >>> Fraction(Decimal(1.1)) Fraction(2476979795053773, 2251799813685248)
The patch looks like what I expected. In the docstring, it would be nice if we kept the line with the spec for decimal strings: [-+]?[0-9]+((/|.)[0-9]+)?
Unfortunately, that line is wrong (or at least incomplete), since decimals in exponential form are also accepted: >>> Fraction('2.3e4') Fraction(23000, 1) I could try to reinstate a fixed version. Attaching a second version of the patch: same as the first, except for some doc tweaks. (Rewording, markup fixes.)