Register Login

What is Polymorphism in Python?

Updated May 03, 2020

In this article, we will learn about polymorphism and various ways to implement it in python.

Define Polymorphism?

The word polymorphism is composed of two words ‘poly’ and ‘morphs’. The word ‘poly’ means many and ‘morphs’ means forms. In short, polymorphism means having many forms.

A real-life example of polymorphism is any person who’s having many different characteristics. Like an employee at office, a husband and a father at home will have different behaviour everywhere.

Polymorphism in Python

Polymorphism helps us in performing many different operations using a single entity. A basic example of polymorphism is a ‘+’ operator. We know we can add as well as concatenate numbers and string respectively. With the help of ‘+’ operator.

Example of Polymorphism in Python 

  1. Polymorphism in ‘+’operators
  2. Polymorphism in ‘*’operators
  3. Polymorphism in Functions
  4. Polymorphism in Classes

Let us understand it more with the help of an example

Example 1: Polymorphism in ‘+’operators

# Polymorphism example in python
# Addition using '+' operator
var1 = 1
var2 = 2
print("Addition of number :",var1+var2)
# Concatenation of string using '+' operator
str1 = 'Hello'
str2 = ' World'
print("Concatenation of string :", str1 + str2)

Output:

Addition of number : 3

Concatenation of string : Hello World

Explanation

In the above example, we used the ‘+’ operator in two different ‘morphs’ or ew can say form.

Example 2: Polymorphism in ‘*’operators.

# Polymorphism example in python
# Multiplication of integer using '*' operator
var1 = 1
var2 = 2
print("multiplication of number :",var1*var2)
# Multiplication of string using '*' operator
var = 2
str2 = 'World '
print("Multiplication of string :", var * str2)

Output

Multiplication of number : 2

Multiplication of string : World World

Explanation

In the above example, we used ‘*’ operator to perform two different operations on different data types. At first, we multiplied two numbers using ‘*’ operator. Then we used the same operator for multiplication of string with an integer. Which resulted in the output shown above.

Example 3:  Polymorphism in Functions

# Function Polymorphism example in python
# Length of string using len()
str = 'Hello'
print("Length of String: ",len(str))
# Length of dictionary using len()
MyDict = {'Name': 'Apoorv', 'Age': 12, 'Class': 6 ''}
print("Length of Dictionary: ",len(MyDict))

Output:

Length of String: 5

Length of Dictionary: 3

Explanation

In the above example, we used a len()which works with different data types. At first, calculated the length of a string. Then we have calculated length of Dictionary. Hence we can conclude that the same function i.e len() is used in 2 different ways. Which is exactly what the definition of polymorphism says.

Example 4: Polymorphism in Classes

Since python is an object-oriented programming language. Thus classes, methods, object are important concepts of OOPs. And here we will learn to implement polymorphism with classes methods having the same name.

class Employee:
    def info(self):
        name = "Rooney"
        dep = "Electronics"
        print(name + " from "+dep)
class Admin:
    def info(self):
        name = "Kalesh"
        dep = "CS"
        print(name + " from "+dep)
obj_emp = Employee()
obj_adm = Admin()
obj_emp.info()
obj_adm.info()

Output:

Rooney from Electronics department

Kalesh from CS department

Explanation

In the above example, we have created two classes Employee and Admin. These two different classes have the same method name info(). This method contains information(name, department) about employee in Employee class and admin in Admin class.

Imagine if there were many hundreds of classes having methods with different name. Then the developer has to remember all of the methods name separately. This is where polymorphism comes to the rescue. Python allows methods of same name in different classes.

Then after initializing classes, we created two objects for respective classes. Then the method info() is called. Once by the object of Employee class and once by the object of Admin class.

NOTE: We can also create an object which will iterate over the methods of Employee and Admin class using for loop. So that we don’t have to call the methods again and again.

Example

obj_emp = Employee()
obj_adm = Admin()
for obj in (obj_emp,obj_adm):
Obj.info() 


×