Difference between int and numbers.Integral in Python

Allow me to add two things:

isinstance(x,numbers.Integral)

also covers long and

isinstance(x, int)

does not. The numbers.Integral test would be closer to

isinstance(x, (int, long))

in Python 2 (Python 3 killed long for good.)

I prefer the test with numbers.Integral, because if you derive from int (or long), isinstance(y, numbers.Integral) will still be True.


numbers defines a hierarchy of abstract classes that define operations possible on numeric types. See PEP 3141. The difference between int and Integral is that int is a concrete type that supports all the operations Integral defines.

Tags:

Python