How to access Parent class Attributes in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
4 min

banner

# Table of Contents

  1. Accessing parent class attributes in Python
  2. Accessing parent class variables
  3. Accessing parent instance variables
  4. Accessing a method that's defined in the Parent class
  5. Get the Name of a Parent class in Python

# Accessing parent class attributes in Python

To access parent class attributes in a child class:

  1. Use the super() method to call the constructor of the parent in the child.
  2. The __init__() method will set the instance variables.
  3. Access any of the parent class's attributes or methods on the self object.
main.py
class Employee(): cls_id = 'emp-cls' def __init__(self, name): self.salary = 100 self.name = name class Developer(Employee): def __init__(self, name): # ๐Ÿ‘‡๏ธ invoke parent __init__() method super().__init__(name) # ๐Ÿ‘‡๏ธ accessing parent instance variable print(self.salary) # ๐Ÿ‘‰๏ธ 100 # ๐Ÿ‘‡๏ธ accessing parent class variable print(self.cls_id) # ๐Ÿ‘‰๏ธ emp-cls d1 = Developer('bobbyhadz') print(d1.salary) # ๐Ÿ‘‰๏ธ 100 print(d1.cls_id) # ๐Ÿ‘‰๏ธ 'emp-cls'

access parent class attributes in python

The code for this article is available on GitHub

The code snippet shows how to access parent class variables and parent instance variables from a child class.

# Accessing parent class variables

The cls_id attribute is a class variable.

Class variables can be accessed directly on an instance of the child or the child class itself.

main.py
class Employee(): cls_id = 'emp-cls' class Developer(Employee): def __init__(self, name): # ๐Ÿ‘‡๏ธ accessing parent class variable print(self.cls_id) # ๐Ÿ‘‰๏ธ emp-cls d1 = Developer('bobbyhadz') print(d1.cls_id) # ๐Ÿ‘‰๏ธ 'emp-cls' print(Developer.cls_id) # ๐Ÿ‘‰๏ธ 'emp-cls'

accessing parent class variables

The code for this article is available on GitHub

# Accessing parent instance variables

To access parent instance variables, call the class's constructor method to run the code in the parent's __init__() method.

main.py
class Employee(): def __init__(self, name): self.salary = 100 self.name = name class Developer(Employee): def __init__(self, name): # ๐Ÿ‘‡๏ธ call parent __init__() method super().__init__(name) print(self.salary) # ๐Ÿ‘‰๏ธ 100 d1 = Developer('bobbyhadz') print(d1.salary) # ๐Ÿ‘‰๏ธ 100

accessing parent instance variables

The code for this article is available on GitHub

The super() method gives us access to the base class without explicitly referring to it.

# Using super() vs the class's name

We could replace the call to super() with Employee to achieve the same result.

main.py
class Employee(): def __init__(self, name): self.salary = 100 self.name = name class Developer(Employee): def __init__(self, name): Employee.__init__(self, name) print(self.salary) # ๐Ÿ‘‰๏ธ 100 d1 = Developer('bobbyhadz') print(d1.salary) # ๐Ÿ‘‰๏ธ 100

using super vs class name

The code for this article is available on GitHub
However, super() is more flexible and more commonly used than explicitly referring to the base class.

The call to the parent's __init__ method runs the method and assigns the salary and name attributes to the instance.

Now we can access the parent class's salary and name attributes on an instance of the child class.

The classes in the example assume that a name argument is required.

Here is the same example, but without passing any arguments when instantiating the child class.

main.py
class Employee(): def __init__(self): self.salary = 100 class Developer(Employee): def __init__(self): super().__init__() print(self.salary) # ๐Ÿ‘‰๏ธ 100 d1 = Developer() print(d1.salary) # ๐Ÿ‘‰๏ธ 100

Once the code in the parent's __init__() method runs, the instance gets assigned a salary attribute, which can be accessed on the self object.

# Accessing a method that's defined in the Parent class

You can use the same approach to access a method defined in the parent class from the child class.

main.py
class Employee(): def __init__(self, name): self.salary = 100 self.name = name def greet(self): print(f'Hello {self.name}') class Developer(Employee): def __init__(self, name): super().__init__(name) print(self.salary) # ๐Ÿ‘‰๏ธ 100 self.greet() # ๐Ÿ‘‰๏ธ Hello bobbyhadz d1 = Developer('bobbyhadz') print(d1.salary) # ๐Ÿ‘‰๏ธ 100 d1.greet() # ๐Ÿ‘‰๏ธ Hello bobbyhadz

accessing method that is defined in parent class

The code for this article is available on GitHub

The parent defines a greet() method which the child instance can access via the self object.

# Get the name of a 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.

# Getting the name of a Parent class when you only have access to an instance

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 code for this article is available on GitHub

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.

# Getting the names of the class's parent classes

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 code for this article is available on GitHub

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

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev