In Python, isinstance(obj, cls) returns True if obj is an instance of clsor of any subclass of it. isinstance(5, int) is True, and so is isinstance(True, int), because bool is a subclass of int.
Pass a tuple of classes to accept any of them: isinstance(x, (int, float)). It also works with abstract base classes from collections.abc, so you can ask βis this iterable?β without listing every concrete type. Since Python 3.10 you can also write int | float.
isinstance() is a built-in function: it lives in the builtins module, so it is always available without an import.
Syntax
Pass the object and a class or a tuple of classes.
python
ok = isinstance(obj, SomeClass)
ok = isinstance(obj, (ClassA, ClassB))
Call
What it does
isinstance(5, int)
Returns True.
isinstance(5, (int, float))
True if it is an int or a float.
isinstance(dog, Animal)
True for subclasses too.
isinstance(x, Iterable)
Uses collections.abc.
Check bool before int when the difference matters, because every bool is also an int.
Library declarations
python
isinstance(object, classinfo, /)
Parameters
Parameter
Description
object
Required. The value to test.
classinfo
Required. A class, a tuple of classes (nested tuples allowed), or on 3.10+ a union like int | str.
Return Value
Return Value
Description
bool
True if the object is an instance of the class or a subclass.
Python Examples
Example 1 — Check Numbers and Strings
Test single classes and tuples of classes, including the bool edge case.