all Python 3numbersterm
numbers.Rational
numeric tower
The abstract class of rational numbers: Real plus numerator and denominator. int, bool and Fraction belong here.
Syntax
isinstance(x, numbers.Rational)
float is not Rational, although every float equals a fraction exactly and offers as_integer_ratio(): it lacks numerator and denominator. int, on the other hand, is Rational: (5).numerator == 5 and (5).denominator == 1. So a function accepting Rational gets int and Fraction, but not 0.5.
Examples
import numbers
from fractions import Fraction
print([isinstance(v, numbers.Rational) for v in (3, Fraction(1, 3), 0.5)]) # → [True, True, False]
print((5).numerator, (5).denominator, (0.5).as_integer_ratio()) # → 5 1 (1, 2)
See also
Open in the full glossarydocs.python.orgReport an inaccuracy