Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

isinstance()

Tests whether an object is an instance of a type or a compatible subclass.

Version: Python 3.14; repository image, production runtime not attested

Syntax

isinstance(object, classinfo, /)

Tests whether an object is an instance of a type or a compatible subclass.

Boolean values pass an integer instance check because the Boolean type inherits from integer.

The test accepts subclasses, so True is an instance of int through bool even though its exact type is bool. A tuple or union of types represents alternatives. The text "1" is neither int nor float; no numeric conversion is performed. A matching earlier type short-circuits a tuple or union; reaching a parameterized generic such as list[int] raises TypeError. Abstract base classes can also recognize registered virtual subclasses.

assert isinstance(True, int | list[int])
try:
    isinstance("x", (int, list[int]))
except TypeError:
    pass
else:
    raise AssertionError
assert type(True) is bool and isinstance(True, int)
assert isinstance("a", str | bytes) and not isinstance("1", (int, float))

Examples

print(isinstance(True, int))
print(isinstance("1", (int, float)))

Result

True
False

The test accepts subclasses, so True is an instance of int through bool even though its exact type is bool. A tuple or union of types represents alternatives. The text "1" is neither int nor float; no numeric conversion is performed. A matching earlier type short-circuits a tuple or union; reaching a parameterized generic such as list[int] raises TypeError. Abstract base classes can also recognize registered virtual subclasses.

Sources