Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

issubclass()

Tests a subclass relationship between classes.

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

Syntax

issubclass(class_, classinfo, /)

Tests a subclass relationship between classes.

Under ordinary subclass checks, a class counts as its own subclass. The first argument must be a class in the ordinary use shown here.

The first argument is a class and the second can be a class, a tuple or a union of classes. bool derives from int, while under ordinary subclass checks a class also counts as its own subclass. This explains both True results. The direction matters: int does not derive from bool. A custom metaclass may return False even for the same class through __subclasscheck__. A tuple or union stops at the first match; reaching list[int] raises TypeError. Registered virtual subclasses may qualify too.

assert issubclass(bool, int | list[int])
try:
    issubclass(str, (int, list[int]))
except TypeError:
    pass
else:
    raise AssertionError
class Meta(type):
    def __subclasscheck__(self, sub): return False
class X(metaclass=Meta): pass
assert not issubclass(X, X)
assert issubclass(bool, int) and issubclass(int, int)
assert issubclass(bool, (str, int)) and not issubclass(int, bool)

Examples

print(issubclass(bool, int))
print(issubclass(int, int))

Result

True
True

The first argument is a class and the second can be a class, a tuple or a union of classes. bool derives from int, while under ordinary subclass checks a class also counts as its own subclass. This explains both True results. The direction matters: int does not derive from bool. A custom metaclass may return False even for the same class through `__subclasscheck__`. A tuple or union stops at the first match; reaching list[int] raises TypeError. Registered virtual subclasses may qualify too.

Sources