This article is a part of a 30 day Python challenge series. You can find the links to all the previous posts of this series here:
- 30 Days of Python 👨💻 - Day One - Introduction
- 30 Days Of Python 👨💻 - Day 2 - Data Types I
- 30 Days of Python 👨💻 - Day 3 - Data Types II
- 30 Days Of Python 👨💻 - Day 4 - Data Types III
- 30 Days Of Python 👨💻 - Day 5 - Conditions And Loops I
- 30 Days Of Python 👨💻 - Day 6 - Loops II And Functions
- 30 Days Of Python 👨💻 - Day 7 - Developer Environment
- 30 Days Of Python 👨💻 - Day 8 - OOP Basics
- 30 Days Of Python 👨💻 - Day 9 - OOP Pillars
I almost explored all the key OOP concepts yesterday. Today I went through the remaining bits and pieces of Object Oriented Programming concepts and their implementation in Python. Along with that, I have tried to include some practical code exercises that involve the overall usage of OOP concepts in Python to recall all the concepts from the mental model that has been developed.
super()
super is a reserved word in Python (was introduced in Python v 2.2) which comes into action during inheritance. When a subclass or child class which inherits from a parent class and needs to call a method of the parent class, it uses super. I know this sounds quite confusing. So here’s an example.
Without using super
- class Employee:
- def __init__(self, name):
- self.name = name
- print(f'{self.name} is an employee')
- class Manager(Employee):
- def __init__(self, department, name):
- self.department = department
- self.name = name
- Employee.__init__(self, name)
- print(f'Manager, {self.department} department')
- staff_1 = Manager('HR', 'Andy')
- # Andy is an employee
- # Manager, HR department
Here, the __init__constructor method of the parent class is called by explicitly using the parent class name and then the self object is passed as the first parameter.
Using super (Compact syntax - No need to pass self)
- class Employee:
- def __init__(self, name):
- self.name = name
- print(f'{self.name} is an employee')
- class Manager(Employee):
- def __init__(self, department, name):
- self.department = department
- self.name = name
- super().__init__(name)
- print(f'Manager, {self.department} department')
- staff_1 = Manager('HR', 'Andy')
- # Andy is an employee
- # Manager, HR department
Just like the constructor method shown in the above code, any method of the parent class can be called inside the child class using super().
In JavaScript, the syntax is more compact where super is called like super (parameter). But I like the Python syntax as well. It is more explicit about calling the __init__ method using super.
Introspection
Python is able to evaluate the type of an object (everything in Python is an object) at runtime. It means the interpreter is able to understand what are the properties and methods of the object and their accessibility at runtime dynamically. This is called introspection.
Python provides a built-in function dir to introspect an object.
- class Developer:
- def __init__(self, name, language):
- self.name = name
- self.language = language
- def introduce(self):
- print(f'Hi! I am {self.name}. I code in {self.language}')
- dev = Developer('Matt', 'Python')
- print(dir(dev)) # Try this in any Python REPL


Roshan RathodPosted Jul 27, 2020, 12:19 PM
Good..keep it up