all Python 3numbersterm
numbers.Integral
numeric tower
The abstract class of integers: Rational plus bitwise operations and __index__. int and bool belong here.
Syntax
isinstance(x, numbers.Integral)
isinstance(True, numbers.Integral) is True: bool subclasses int, so an "is it an integer" check lets flags through. If you do not want them, exclude bool explicitly. Conversely, 3.0 is not Integral even though (3.0).is_integer() is true: the type is tested, not the value. Libraries such as NumPy register their integers here, which makes Integral more robust than isinstance(x, int).
Examples
import numbers
print(isinstance(True, numbers.Integral), isinstance(3.0, numbers.Integral)) # → True False
def is_count(x):
return isinstance(x, numbers.Integral) and not isinstance(x, bool)
print(is_count(5), is_count(True)) # → True False
See also
Open in the full glossarydocs.python.orgReport an inaccuracy