Python Forum

Full Version: fraction module: can you stop the reducing?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
when you import the fraction module, you can instantiate a Fraction instance:

from fractions import Fraction as frac
a = frac(1,3)
b = frac(1,6)
c = a+b
print('sum is {0}'.format©)

1/2

But I would like have the reducible form of 3/6

Is there a switch I'm missing in rationals, fractions, etc? that would cause the fraction to NOT be put in irreducible form?

Tnx
No, not really.

There's an internal _normalize flag that can be set, allowing the creation of non-normalized fractions (and probably leading to much undefined behavior).  

>>> frac(3, 6, _normalize=False)
Fraction(3, 6)
But it wouldn't do anything for the results of calculations.  Part of the use of the module is to allow equal fractions to be found equal, and that's done through normalization.  So it looks to be a core part that can't be disabled.

>>> frac(1,2) == frac(3,6, _normalize=False)
False