Here's a summary of the different methods mentioned here:
int(x) == x
try x = operator.index(x)
isinstance(x, int)
isinstance(x, numbers.Integral)
and here's how they apply to a variety of numerical types that have integer value:
You can see they aren't 100% consistent. Fraction and Rational are conceptually the same, but one supplies a .index()
method and the other doesn't. Complex types don't like to convert to int even if the real part is integral and imaginary part is 0.
(np.int8|16|32|64(5)
means that np.int8(5)
, np.int32(5)
, etc. all behave identically)