Get all base classes of a class in Python

avatar

Borislav Hadzhiev

Last updated: Sep 13, 2022

banner

Photo from Unsplash

Get all base classes of a class in Python #

Use the inspect.getmro() method to get all of the base classes of a given class, e.g. print(inspect.getmro(Developer)). The getmro() method returns a tuple containing the base classes of the provided class.

main.py
import inspect class Person(): pass class Employee(Person): pass class Developer(Employee): pass # 👇️ (<class '__main__.Developer'>, <class '__main__.Employee'>, <class '__main__.Person'>, <class 'object'>) print(inspect.getmro(Developer)) d1 = Developer() # 👇️ (<class '__main__.Developer'>, <class '__main__.Employee'>, <class '__main__.Person'>, <class 'object'>) print(inspect.getmro(type(d1))) # 👇️ (<class '__main__.Employee'>,) print(Developer.__bases__)

We used the inspect.getmro method to get a tuple of the base classes of a given class.

MRO is an abbreviation for method resolution order.

The provided class is the first element in the tuple and no class appears more than once.

If you only have access to an instance of the class, pass the instance to the type() class in the call to the getmro() method.

main.py
import inspect class Person(): pass class Employee(Person): pass class Developer(Employee): pass d1 = Developer() # 👇️ (<class '__main__.Developer'>, <class '__main__.Employee'>, <class '__main__.Person'>, <class 'object'>) print(inspect.getmro(type(d1)))

The type class returns the type of an object.

Most commonly the return value is the same as accessing the __class__ attribute on the object.

Classes also have a __mro__ attribute.

main.py
class Person(): pass class Employee(Person): pass class Developer(Employee): pass # 👇️ (<class '__main__.Developer'>, <class '__main__.Employee'>, <class '__main__.Person'>, <class 'object'>) print(Developer.__mro__)

The class.__mro__ attribute returns a tuple of the classes that are considered when looking for base classes during method resolution.

Classes have a mro() method that is called at class instantiation. The output of the mro() method is stored in the __mro__ attribute.

main.py
class Person(): pass class Employee(Person): pass class Developer(Employee): pass # 👇️ [<class '__main__.Developer'>, <class '__main__.Employee'>, <class '__main__.Person'>, <class 'object'>] print(Developer.mro())

The class.mro method can be overridden by a metaclass to customize the method resolution order for the class's instances, but it's not something you'll often have to do.

If you only need to get the immediate base classes of a given class, use the class.__bases__ attribute.

main.py
class Person(): pass class Employee(Person): pass class Developer(Employee): pass # 👇️ (<class '__main__.Employee'>,) print(Developer.__bases__)

The class.__bases__ attribute returns a tuple of the immediate base classes of the given class.

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.