class.__subclasscheck__(self, subclass) method implements the
issubclass(subclass, class) built-in function. It should return
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.
In the following example, you create custom classes
Friend inherit from
Child inherits from
Parent. Thus, per transitive inheritance,
Child inherits from
Parent as well. However,
Child doesn’t inherit from
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
__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!
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
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:
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:
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.