Python Glossary Русский

all Python 3numbersterm

numbers

module

An abstract hierarchy of numeric types: Number, Complex, Real, Rational, Integral, used with isinstance checks.

Syntax

import numbers

numbers is for functions that accept "any integer" or "any real number" rather than int or float specifically: isinstance(x, numbers.Integral) admits int and third-party integer types alike. The classes are abstract and you do not instantiate them. Traps: bool is Integral too, and Decimal is not registered as Real, while Fraction is Rational.

Examples

import numbers
from decimal import Decimal
from fractions import Fraction
print(isinstance(True, numbers.Integral))  # → True
print(isinstance(2.5, numbers.Real), isinstance(Decimal('2.5'), numbers.Real))  # → True False
print(isinstance(Fraction(1, 3), numbers.Rational))  # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy