Python Glossary Русский

all Python 3numbersterm

numbers.Number

numeric tower

The root of the numeric abstract class hierarchy: isinstance(x, numbers.Number) asks "is this a number at all".

Syntax

isinstance(x, numbers.Number)

The hierarchy is Number → Complex → Real → Rational → Integral. Number requires no methods at all, so numbers.Number() can be instantiated, and isinstance(x, Number) is true for int, bool, float, complex, Fraction and Decimal. The string '5' is not a number. A custom type joins via Number.register(MyType), with no check that it can do anything.

Examples

import numbers
from decimal import Decimal
from fractions import Fraction
values = [True, 3, 2.5, 1j, Fraction(1, 3), Decimal('1.5'), '5']
print([isinstance(v, numbers.Number) for v in values])  # → [True, True, True, True, True, True, False]
import numbers
print(numbers.Integral.__mro__[1].__name__, numbers.Rational.__mro__[1].__name__, numbers.Real.__mro__[1].__name__, numbers.Complex.__mro__[1].__name__)  # → Rational Real Complex Number

See also

Open in the full glossarydocs.python.orgReport an inaccuracy