[Tutor] How to extract numerator and denominator from fractions.Fraction(4, 32)?

eryksun eryksun at gmail.com
Tue Aug 6 06:21:43 CEST 2013


On Mon, Aug 5, 2013 at 11:50 PM, Jim Mooney <cybervigilante at gmail.com> wrote:
> Or, to make it even more usable and get numbers back:
>
> f = [int(x) for x in str(fractions.Fraction(6,21)).split('/')]
> [2, 7]

This looks like a job for IterableFraction:

    class IterableFraction(Fraction):
        def __iter__(self):
            yield self.numerator
            yield self.denominator

    >>> list(IterableFraction(6, 21))
    [2, 7]

j/k :)


More information about the Tutor mailing list