Accessing parent class attributes in Python

avatar

Borislav Hadzhiev

Last updated: Sep 13, 2022

banner

Photo from Unsplash

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'

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

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'

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

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

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
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 classes' 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.

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

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

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.