Python __subclasscheck__() Magic Method

Rate this post

Syntax

class.__subclasscheck__(self, subclass)

Python’s class.__subclasscheck__(self, subclass) method implements the issubclass(subclass, class) built-in function. It should return True if subclass is a direct or indirect subclass of class using Python inheritance to define the subclass relationship.

We call this a “Dunder Method” for Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check out our dunder cheat sheet article on this blog.

Example

In the following example, you create custom classes Person, Friend, Parent, and Child. Both Parent and Friend inherit from Person. Child inherits from Parent. Thus, per transitive inheritance, Child inherits from Parent as well. However, Child doesn’t inherit from Friend.

class Person:
    pass

class Friend(Person):
    pass

class Parent(Person):
    pass

class Child(Parent):
    pass


# Is Child a Parent? Yes!
print(issubclass(Child, Parent))
# True

# Is Child a Friend? No!
print(issubclass(Child, Friend))
# False

# Is Child a Person? Yes!
print(issubclass(Child, Person))
# True

The __subclasscheck__() dunder method is implemented implicitly and by default. That’s why you don’t need to define it explicitly. However, if you do so, you can manipulate the output of these function calls!

To override the __subclasscheck__ magic method, you need to define it on the metaclass as exemplified in PEP 3119.

This is how you’d override the __subclasscheck__ magic method for our example to now return False, no matter what:

class PersonMeta(type):
    def __subclasscheck__(self, subclass):
        return False
    
class Person(metaclass=PersonMeta):
    pass

class Friend(Person):
    pass

class Parent(Person):
    pass

class Child(Parent):
    pass


# Is Child a Person? Not anymore!
print(issubclass(Child, Person))
# False

Note that the same statement print(issubclass(Child, Person)) previously printed True to the standard output—but with the override, it now prints False.

Background issubclass()

Python’s built-in issubclass(X, Y) function takes a class X and a class Y and returns True if the X is an instance of Y and otherwise False. The argument Y can also be a tuple in which case it checks whether X is a subclass of any class in the tuple—such as in issubclass(X, (class_1, class_2, ...)).

Learn more in our detailed article:

Background Inheritance

Inheritance allows you to define a class that inherits all methods and properties from another class.

  • Parent class, also denoted as base class, is the class you inherit from. In Python, every class can be a parent class.
  • Child class, also denoted as derived class, inherits from the Parent class. In Python, you can create a child class that inherits all methods and attributes from the Parent using the class Child(Parent) syntax with the parent class enclosed in parentheses.

You can watch our related video on inheritance here:

Learn more in our detailed articles: