Get name of Parent class in Python

avatar

Borislav Hadzhiev

Last updated: Sep 13, 2022

banner

Photo from Unsplash

Get name of Parent class in Python #

To get the name of a parent class:

  1. Use the __bases__ attribute on the class to get a tuple of parent classes.
  2. Use a for loop to iterate over the tuple.
  3. Use the __name__ attribute on each class to get the name of the parent classes.
main.py
class Person(): pass class Employee(Person): pass class Developer(Employee): pass immediate_parents = Developer.__bases__ print(immediate_parents) # 👉️ (<class '__main__.Employee'>,) for parent in immediate_parents: print(parent.__name__) # 👉️ Employee print(immediate_parents[0].__name__) # 👉️ Employee

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 d1 = Developer() immediate_parents = type(d1).__bases__ print(immediate_parents) # 👉️ (<class '__main__.Employee'>,) for parent in immediate_parents: print(parent.__name__) # 👉️ Employee print(immediate_parents[0].__name__) # 👉️ Employee

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.

You can use the same approach to get the names of the class's parent classes inside of the class.

main.py
class Person(): pass class Employee(Person): pass class Developer(Employee): def get_parent_class_names(self): immediate_parents = self.__class__.__bases__ print(immediate_parents) for parent in immediate_parents: print(parent.__name__) d1 = Developer() # (<class '__main__.Employee'>,) # Employee d1.get_parent_class_names()

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

If you need to get the names of 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 base_classes = inspect.getmro(Developer) # 👇️ (<class '__main__.Developer'>, <class '__main__.Employee'>, <class '__main__.Person'>, <class 'object'>) print(base_classes) for base in base_classes: # Developer # Employee # Person # object print(base.__name__)

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 d1 = Developer() base_classes = inspect.getmro(type(d1)) # 👇️ (<class '__main__.Developer'>, <class '__main__.Employee'>, <class '__main__.Person'>, <class 'object'>) print(base_classes) for base in base_classes: # Developer # Employee # Person # object print(base.__name__)

Classes also have a __mro__ attribute.

main.py
class Person(): pass class Employee(Person): pass class Developer(Employee): pass base_classes = Developer.__mro__ # 👇️ (<class '__main__.Developer'>, <class '__main__.Employee'>, <class '__main__.Person'>, <class 'object'>) print(base_classes) for base in base_classes: # Developer # Employee # Person # object print(base.__name__)

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 base_classes = Developer.mro() # 👇️ [<class '__main__.Developer'>, <class '__main__.Employee'>, <class '__main__.Person'>, <class 'object'>] print(base_classes) for base in base_classes: # Developer # Employee # Person # object print(base.__name__)

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.