<<< Prev

Next >>>

Inheritance in Python

Python inheritance and types of inheritance in python with their syntax. Moreover, we will study Python super function, Python method overriding and Python method overloading.

So, let’s start the Python Inheritance Tutorial.

 It is a universal fact that every student is a person. This is in hindsight of non-human students if any. To depict this relationship, we take an illustration.

The relationship from Animal to the tiger is termed ‘Specialization’. Conversely, every Tiger is an animal, this is called Generalization. In this representation, we use an arrow towards the base class as a UML (Unified Modeling Language) convention.
Here, Animal can be called any of the following:

Super Class

Parent Class

Base Class

Likewise, Tiger  here is:

Sub Class

Child Class

Derived Class

Python Inheritance Syntax:

To make a class inherit from another, we apply the name of the base class in parentheses to the derived class’ definition.

>>> class Animal:
>>> class Tiger(Animal):
>>> issubclass(Tiger Animal)


Here, class Tiger inherits from the class Animal. Here, since we only want to focus on the python syntax, we use the ‘pass’ statement in the bodies of the classes. Also, we use the function issubclass() to confirm that Tiger  is a subclass of Animal.

In Python, there are 5 types of Inheritance: 

  1. Single inheritance
  2. Hybrid inheritance
  3. Multiple inheritance
  4. Multilevel inheritance
  5. Hierarchical inheritance


Single Inheritance in Python

A single Python inheritance is when a single class inherits from a class.

1.	>>> x=0
2.	>>> class fruit:
3.	def __init__(self):
4.	global x
5.	x+=1
6.	print("I'm a fruit") 
7.	>>> class citrus(fruit):
8.	def __init__(self):
9.	super().__init__()
10.	global x
11.	x+=2
12.	print("I'm citrus") 
13.	>>> x


Python Multiple Inheritance

Multiple Python inheritance are when multiple python classes inherit from a class.

1.	>>> class Color:
2.	pass
3.	>>> class Fruit:
4.	pass
5.	>>> class Orange(Color,Fruit):
6.	pass
7.	>>> issubclass(Orange,Color) and issubclass(Orange,Fruit)


Multilevel Inheritance in Python

When one class inherits from another, which in turn inherits from another, it is multilevel python inheritance.

1.	>>> class A:
2.	x=1 
3.	>>> class B(A):
4.	pass
5.	>>> class C(B):
6.	pass
7.	>>> cobj=C()
8.	>>> cobj.x


Hierarchical Inheritance in Python

When more than one class inherits from a class, it is hierarchical Python inheritance.

1.	>>> class A:
2.	pass
3.	>>> class B(A):
4.	pass
5.	>>> class C(A):
6.	pass
7.	>>> issubclass(B,A) and issubclass(C,A)


Hybrid Inheritance in Python

Hybrid Python inheritance is a combination of any two kinds of inheritance.

1.	>>> class A:
2.	x=1 
3.	>>> class B(A):
4.	pass
5.	>>> class C(A):
6.	pass
7.	>>> class D(B,C):
8.	pass
9.	>>> dobj=D()
10.	>>> dobj.x

Python Inheritance Super Function – Super()

With inheritance, the super() function in python actually comes in quite handy. It allows us to call a method from the parent class. Let’s define a new class for this.

1.	>>> class Vehicle:
2.	def start(self):
3.	print("Starting engine")
4.	def stop(self):
5.	print("Stopping engine") 
6.	>>> class TwoWheeler(Vehicle):
7.	def say(self):
8.	super().start()
9.	print("I have two wheels")
10.	super().stop() 
11.	>>> Pulsar=TwoWheeler()
12.	>>> Pulsar.say()

Starting engine
I have two wheels
Stopping engine


Python Override Method

A subclass may change the functionality of a Python method in the superclass. It does so by redefining it. This is termed python method overriding. Lets see this Python Method Overriding Example.

1.	>>> class A:
2.	def sayhi(self):
3.	print("I'm in A") 
4.	>>> class B(A):
5.	def sayhi(self):
6.	print("I'm in B") 
7.	>>> bobj=B()
8.	>>> bobj.sayhi()

I'm in B


Python Method Overloading

Before we say anything, we’d like you to take a look at the following code:

1.	>>> def add(a,b):
2.	return a+b
3.	>>> def add(a,b,c):
4.	return a+b+c
5.	>>> add(2,3)
Traceback (most recent call last):
 File "<pyshell#8>", line 1, in <module>
TypeError: add() missing 1 required positional argument: 'c'


What looks like overloading methods, it is actually that Python keeps only the latest definition of a method you declare to it. This code doesn’t make a call to the version of add() that takes in two arguments to add. So we find it safe to say Python doesn’t support method overloading. However, we recently ran into a rather Pythonic way to make this happen. Check this out:

1.	>>> def add(instanceOf,*args):
2.	if instanceOf=='int':
3.	result=0
4.	if instanceOf=='str':
5.	result=''
6.	for i in args:
7.	result+=i
8.	return result


In this code, not only do we use the *args magic variable for variable arity, we also let the code deal with both integers and strings. Watch it happen:

1.	>>> add('int',3,4,5)
1.	>>> add('str','I ','speak ','Python')

'I speak Python'

You say what if I do this ?:

1.	>>> def add(a,b,c=0):
return a+b+c
1.	>>> add(2,3)


To that, we’ll say this isn’t method overloading, this is simply used of default arguments.