Find all subclasses of a class in Python

avatar

Borislav Hadzhiev

Last updated: Sep 13, 2022

banner

Photo from Unsplash

Find all subclasses of a class in Python #

Use the class.__subclasses__() method to find all subclasses of a class. The __subclasses__ attribute returns a list of the immediate subclasses of the given class.

main.py
class Person(): pass class Employee(Person): pass class Developer(Person): pass subclasses = Person.__subclasses__() # 👇️ [<class '__main__.Employee'>, <class '__main__.Developer'>] print(subclasses) # ✅ get immediate subclasses of class names_of_subclasses = [ subclass.__name__ for subclass in Person.__subclasses__()] print(names_of_subclasses) # 👉️ ['Employee', 'Developer']
Note that the class.__subclasses__() method returns only the immediate subclasses of the given class. If you need to get immediate and nested subclasses, scroll down to the next code snippet.

We used the __subclasses__() method to get a list of the immediate subclasses of a class.

If you only need the names of the subclasses and not the actual objects, use a list comprehension to iterate over the subclasses list.

The following function finds all subclasses of a class, not just the immediate ones.
main.py
class Person(): pass class Employee(Person): pass # 👇️ subclass of Employee (not Person) class Developer(Employee): pass def get_all_subclasses(cls): subclasses = [] for subclass in cls.__subclasses__(): subclasses.append(subclass) subclasses.extend(get_all_subclasses(subclass)) return subclasses all_subclasses = get_all_subclasses(Person) # 👉️ [<class '__main__.Employee'>, <class '__main__.Developer'>] print(all_subclasses) names_of_subclasses = [subclass.__name__ for subclass in all_subclasses] print(names_of_subclasses) # 👉 ['Employee', 'Developer']

Notice that the Developer class is a subclass of Employee, not of Person.

The get_all_subclasses function takes a class and recursively uses the __subclasses__() method to find all its subclasses.

We first append the subclass to the subclasses list and then extend the list with the result of calling get_all_subclasses() with the subclass.

The function then gets the nested subclasses and appends them to the list.

If you use multiple inheritance and need to avoid duplicate entries in the subclasses list, use a set object instead.

main.py
class Person(): pass class Employee(Person): pass class Developer(Employee, Person): pass def get_all_subclasses(cls): subclasses = set() for subclass in cls.__subclasses__(): subclasses.add(subclass) subclasses.update(get_all_subclasses(subclass)) return list(subclasses) all_subclasses = get_all_subclasses(Person) # 👉️ [<class '__main__.Employee'>, <class '__main__.Developer'>] print(all_subclasses)

Set objects are an unordered collection of unique elements, so there is a guarantee that there won't be any duplicates in the set.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.