How to Access Parent Class Attributes and Methods in Python
In object-oriented programming, inheritance allows child classes to inherit and access attributes and methods from their parent classes.
This guide explains how to access parent class members (variables and methods) from within a child class in Python, using super()
and direct access.
Accessing Parent Class Instance Variables
To access instance variables initialized in the parent class, you must call the parent's __init__
method from within the child's __init__
. The best way to do this is with super()
:
class Employee():
cls_id = 'emp-cls' # Class variable
def __init__(self, name):
self.salary = 100 # Instance variable
self.name = name # Instance variable
class Developer(Employee):
def __init__(self, name):
super().__init__(name) # Call parent's __init__
# Now we can access parent's instance variables:
print(self.salary) # Output: 100
print(self.name) # Output: tomnolan
d1 = Developer('tomnolan')
print(d1.salary) # Output: 100
- The
super().__init__(name)
line calls the parent class's constructor (__init__
method). - This initializes the instance variables defined in the
Employee
class (self.salary
andself.name
), making them accessible to theDeveloper
instance.
You can technically call the parent's __init__
directly using Employee.__init__(self, name)
, but super()
is preferred for its better handling of multiple inheritance and more general flexibility.
Accessing Parent Class Variables (Class-Level Attributes)
Class variables (attributes defined directly within the class, outside of any method) are accessible directly on the instance or on the class itself.
class Employee():
cls_id = 'emp-cls' # Class variable
class Developer(Employee):
def __init__(self, name):
print(self.cls_id) # Accessing parent class variable using self
#OR
print(Developer.cls_id) # Accessing parent class variable using class
d1 = Developer('tomnolan') # Output: emp-cls
print(d1.cls_id) # Output: emp-cls (accessing through instance)
print(Developer.cls_id) # Output: emp-cls (accessing through class)
- Class variables are shared among all instances of the class and its subclasses.
Accessing Parent Class Methods
To call methods defined in the parent class, you can use super()
again:
class Employee():
def __init__(self, name):
self.name = name
self.salary = 100
def greet(self):
print(f'Hello {self.name}')
class Developer(Employee):
def __init__(self, name):
super().__init__(name)
print(self.salary)
self.greet() # Call parent's method
d1 = Developer('tomnolan')
# Output:
# 100
# Hello tomnolan
print(d1.salary) # Output: 100
d1.greet() # Output: Hello tomnolan
Output:
100
Hello tomnolan
100
Hello tomnolan
- Within the
Developer
class,self.greet()
calls thegreet
method defined in theEmployee
class.self
is automatically passed to the parent's method.
Getting the Name of a Parent Class
From the Child Class
To get the name(s) of the immediate parent class(es) from within the child class, use the __bases__
attribute of the class object.
class Person():
pass
class Employee(Person):
pass
class Developer(Employee):
def get_parent_class_names(self):
immediate_parents = self.__class__.__bases__
print(immediate_parents) # Output: (<class '__main__.Employee'>,)
for parent in immediate_parents:
print(parent.__name__) # Employee
d1 = Developer()
d1.get_parent_class_names()
self.__class__.__bases__
returns the parent of the current class, not an instance.
From an Instance of the Child Class
To get the parent class name from an instance of the child class:
class Person():
pass
class Employee(Person):
pass
class Developer(Employee):
pass
d1 = Developer()
immediate_parents = type(d1).__bases__
#or
immediate_parents = d1.__class__.__bases__
print(immediate_parents) # Output: (<class '__main__.Employee'>,)
print(immediate_parents[0].__name__) # Output: Employee
type(d1)
gets the class of the instance (which isDeveloper
).. __bases__
is then accessed on the class, not the instance, to retrieve a tuple of base classes.[0]
accesses the first parent class (in this case, there's only one)..__name__
gets the name of the class as a string.