Next Tutorial >> super keyword in Python
Inheritance is one of the most important concept in object-oriented programming. Inheritance allows us to reuse the properties of existing class into the other class.
Main idea behind the Inheritance is that a child class acquires all the properties of its parent class and can access all the data members and functions of the parent class.
In programming terminology, we call parent class as base class and child class as derived class.

Achieving Inheritance in Python
We can implement Inheritance in following way :
class Derived(Base)
Above code means, class Derived is inheriting all the properties (data members, methods) of its parent class Base.
A class can also inherit properties from multiple classes.
class Derived(Base1, Base2, Base3)
Let’s consider one example to showcasing Inheritance,
class Base:
def display(self):
print('Inside Base class')
class Derived(Base): # Derived class inheriting Base class
def get_method_from_base_class(self):
self.display() # calling display method from Base class
d = Derived() # creating object for Derived class
d.get_method_from_base_class() # calling method of Derived class
O/P
Inside Base class
Please note that, if we try to access the member without inheriting the class, then it will result in
AttributeError: ‘Derived’ object has no attribute ‘display’
Constructors in Inheritance
As of now, we know that when a derived class inherits the base class, then, all the variables and methods of base class become accessible from derived class.
But, what about constructors ?
Let’s see whether that can be inherited or not,
class Programming: # base class
def __init__(self):
self.lang = 'Python'
class Python(Programming):
def display(self):
print('Language of Mars: ', self.lang) # trying to access self.lang from Programming class
p = Python()
p.display()
O/P
Language of Mars: Python
Hence, from the above code, it is clear that the constructor of the base class is accessible from the sub-class(derived class).
Please note that, writing a constructor in the derived class (subclass) will override the constructor of base class(parent class). In this case, parent class constructor will not be available.
Let’s try to alter the above code itself
class Programming: # base class
def __init__(self):
self.lang = 'Python'
class Python(Programming):
def __init__(self): # defining constructor for this subclass
self.no_lang = 'Python'
def display(self):
print('Language of Mars: ', self.lang) # trying to access self.lang from Programming class
p = Python()
p.display()
O/P
AttributeError: ‘Python’ object has no attribute ‘lang’
Solution to above problem is to make use of super keyword in Python.
Types of Inheritance in Python
1. Single Inheritance
Single Inheritance is the type of Inheritance that comes into picture when a sub class inherits the properties of its parent class.

In Single Inheritance, there can be only one base class, but there can be ‘n‘ number of subclasses derived from it.
Syntax
class A:
# statements
class B(A):
# statements
class C(A):
# statements
...
Code below is the example of Single Inheritance.
class Backend:
def __init__(self):
self.statusCode = 200
class Api(Backend):
def return_body(self):
if self.statusCode == 200:
return {'statusCode': 200, 'body': "OK"};
else:
return {'statusCode': 500, 'body': "NOT OK"};
api = Api()
response = api.return_body()
print(response)
Here, we have defined two classes :
Backend: Parent class
Api: Child class
We are trying to access ‘self.statusCode’ variable from parent class into our child class.
This is the clear cut example of Single Inheritance.
2. Multilevel Inheritance
Just like other object oriented languages, Multilevel Inheritance is valid in Python as well.
In Multilevel Inheritance, there is a hierarchy where lower level class inherits the upper level class. For example,
class D inherits class C. (C is the parent of D)
class C inherits class B. (B is the parent of C)
class B inherits class A. (A is the parent of B)

Syntax
class A:
# statements
class B(A):
# statements
class C(B):
# statements
class D(C):
# statements
...
Code below is the example of Multilevel Inheritance.
class Lovers:
def post(self):
print('He loves she')
class Haters(Lovers):
def send(self):
self.post() # from Lovers
print('He hates she')
class Introverts(Haters):
def write(self):
self.send() # from Haters
print('He is a programmer')
l = Introverts()
l.write()
O/P
He loves she
He hates she
He is a programmer
3. Multiple Inheritance
Python supports Multiple Inheritance.
In Multiple Inheritance, a derived class can have more than one base class.
In other words, there will be more than one parent class and there may be one or more child classes.

Syntax
class A:
# statements
class B:
# statements
class C:
# statements
class D(A, B, C):
# statements
...
Code below is the example of Multiple Inheritance.
Problem with Multiple Inheritance: Check here
class Father:
def father(self):
print('calling Father')
class Mother:
def mother(self):
print('calling Mother')
class Son(Father, Mother):
def son(self):
self.father() # from Father
self.mother() # from Mother
print('calling son')
s = Son()
s.son()
O/P
calling Father
calling Mother
calling son
Well, that’s all about Inheritance in Python.

Nice article, but to complete this basic picture you really should have include example with super() inheritance of constructor 🙂
Thanks for your appreciation. We have a dedicated article for super() as well: http://codeofgeeks.com/super-statement-in-python-with-examples/#problem-with-multiple-inheritance