To accept integers and their subclasses but reject True and False, use isinstance(value, int) and not isinstance(value, bool). If you want only the exact built-in int type, use type(value) is int. The right check depends on whether integer subclasses should count.
How do I check if a variable is an integer in Python?
Choose the check that matches your rule:
- Accept
intsubclasses but exclude booleans:isinstance(value, int) and not isinstance(value, bool). - Accept only the exact built-in
inttype:type(value) is int.
For example:
value = 42
is_integer = isinstance(value, int) and not isinstance(value, bool)
is_exact_builtin_int = type(value) is int
isinstance(value, int) accepts instances of int and its subclasses. Since bool is itself a subclass of int, that test alone also accepts True and False. The first expression excludes booleans while still allowing other int subclasses. The second excludes booleans and all other subclasses, so use it only when exact type identity is required. See Python’s boolean type documentation and documentation for isinstance.
Why does isinstance(True, int) return True?
Because Python defines bool as a subclass of int. Consequently, True and False behave like the integers 1 and 0 in many numeric contexts, and both satisfy isinstance(value, int). That inheritance is part of Python’s type semantics, not an error in isinstance.
PEP 285, “Adding a bool type,” explains the compatibility rationale: “Because bool inherits from int, True+1 is valid and equals 2, and so on.” If your application means an integer but not a truth value, add the explicit boolean exclusion.
#1 Best Overall
When should I use operator.index() instead?
Use operator.index(value) when your code needs to accept objects that implement Python’s lossless integer-index protocol, rather than test whether the original object has a particular type. For example, Python uses this protocol for operations such as indexing and the built-in bin, hex, and oct functions. The data model describes __index__ as the method for this lossless integer conversion: Python data model: object.__index__.
Call the function and handle TypeError if the object does not support the protocol:
Rank #2
import operator
try:
index_value = operator.index(value)
except TypeError:
# value does not support the integer-index protocol
pass
else:
# index_value is the resulting integer
pass
This is a conversion attempt, not a test that the original value is exactly a built-in int. It answers a different question from either isinstance or type(value) is int.
Why not use int(value) as the check?
int(value) converts a value when possible; it does not tell you whether the original object was an integer. A successful conversion therefore does not establish that the input was an int instance. Decide whether you need subclass membership, exact type identity, or integer-index protocol support, then use the corresponding check.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




