Polymorphism in Python

In python, polymorphism means providing an ability to take more than one form, and it’s the main concept of object-oriented programming.

 

Generally, polymorphism is a combination of two words: poly and another one is morphs. Here poly means “multiple” and morphs means “forms” so polymorphism means many forms.

 

The polymorphism will provide an ability to perform different tasks with a single type entity such as method, operator, etc. For example, the python built-in len() function is useful to perform various operations, as shown below.

Python Polymorphism Example

Following is the example of len() function to perform different tasks in python.

 

print("Length of String: {}".format(len("Welcome to Tutlane")))
print("No. of Items in List: {}".format(len(["Suresh", "Rohini", "Trishi", "Hanshu"])))
print("No. of Keys in Dict: {}".format(len({"Name": "Suresh", "Location": "Hyderabad"})))

If you observe the above python example, the same len() function is used to perform different operations such as calculating the length of string, counting the numbers of items in the list, getting the number of keys in the dictionary, etc., based on the requirements.

 

When you execute the above example, you will get the result as shown below.

 

Length of String: 18
No. of Items in List: 4
No. of Keys in Dict: 2

You can also create your own functions to implement polymorphism in python as shown below.

 

def addition(a, b, c = 0):
  print("sum = {}".format(a + b + c))

addition(10, 20)
addition(30, 40, 50)

If you observe the above python example, we created the addition function and able to call the same function by passing different parameters.

 

Python won’t allow creating multiple methods with the same name but with different parameters. Due to that, we used default arguments. To learn more about it, refer to Method Overloading in Python.

 

The above python polymorphism example will return the result as shown below.

 

sum = 30
sum = 120

Polymorphism in Python with Classes

In python, the polymorphism will provide an ability to the classes to create different methods that are called with the same name.

 

Following is the example of implementing the polymorphism in python with classes.

 

class Name:
   def getDetails(self, info):
     print("Name: {}".format(info))

class Location:
   def getDetails(self, info):
     print("Location: {}".format(info))

u1 = Name()
u2 = Location()
u1.getDetails("Suresh Dasari")
u2.getDetails("Hyderabad")

If you observe the above example, we created different classes (Name, Location) to have methods with the same name (getDetails) in python.

 

The above python polymorphism example will return the result as shown below.

 

Name: Suresh Dasari
Location: Hyderabad

Polymorphism in Python with Inheritance

As we learned in python inheritance, we can inherit all the base class properties and methods in child class based on our requirements.

 

In python, the polymorphism also provides an ability to create the child class methods with the same parent class method names and signature to modify the functionality of base class methods in child class.

 

In python, if you override a base class method in child class by creating the method with same name and signature we will call it method overriding.

 

Following is the example of implementing the polymorphism in python with inheritance.

 

#Base/Parent Class
class user:

   def getinfo(self):
     print("Learn Python")

#Child/Derived Class
class Employees(user):

   def getinfo(self):
     print("Welcome to Tutlane")

e1 = Employees()
e1.getinfo()

The above python polymorphism example will return the result as shown below.

 

Welcome to Tutlane 

This is how you can implement polymorphism in python classesfunctions, etc., based on your requirements.