Get the parent classes of a given class in Python

avatar

Borislav Hadzhiev

Last updated: Sep 13, 2022

banner

Photo from Unsplash

Get the parent classes of a given class in Python #

Use the __bases__ attribute to get the parent classes of a class, e.g. print(Developer.__bases__). The __bases__ attribute returns a tuple containing the immediate parent classes of the provided class.

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

We used the class.__bases__ attribute to get a tuple of the parent classes of a class.

If you only have access to an instance of the class, access the __bases__ attribute on the result of calling the type() class with the instance.
main.py
class Person(): pass class Employee(Person): pass class Developer(Employee): pass # 👇️ (<class '__main__.Employee'>,) d1 = Developer() print(type(d1).__bases__)

The __bases__ attribute only returns the immediate parent classes of the given class.

If you need to get all ancestors of a class, use the inspect.getmro() method.

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))

MRO is an abbreviation for method resolution order.

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

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 # 👇️ (<class '__main__.Employee'>,) 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.

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.