predict is int
checks whether the integer has the same identity as the integer type object. It does not. That integer's type, however, is equal to the integer type object. Check whether the integer's type is equal to the integer type object.
>>> type(2) == int
True
You can expand this check to also handle subclasses with the built-in function isinstance()
:
>>> isinstance(2, int)
True
>>> isinstance(True, int)
True
The second test checks whether True
is an instance of an integer. Since bool
is a subclass of int
, this is accurate. This is useful for user-defined classes - if you have a Person
class with a Customer
subclass, isinstance()
will tell you that a Customer
object is also a Person
. However, not all Person
objects are Customer
objects, so it is not commutative (as shown below with bool
and an integer):
>>> isinstance(2, bool)
False