Last updated: Apr 9, 2024
Reading timeยท4 min
To access parent class attributes in a child class:
super()
method to call the constructor of the parent in the child.__init__()
method will set the instance variables.self
object.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.
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.
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.
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
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.
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.
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.
To get the name of a parent class:
__bases__
attribute on the class to get a tuple of parent classes.for
loop to iterate over the tuple.__name__
attribute on each class to get the name of the parent
classes.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.
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.
__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.
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.
You can learn more about the related topics by checking out the following tutorials: