Polymorphism in Python

  • December 16, 2022
  • python
JOIN Clause
What is Polymorphism?

Polymorphism is derived from the Greek words poly (many) and morphism (change) (forms). That is, the same function name can be used for multiple types. This makes programming more intuitive and straightforward.

Polymorphism can be defined in a variety of ways in Python. So, let’s start and look at how polymorphism works in Python.

Example of in-built polymorphic functions:

# len() function is used for a string  
print (len("coding is fun "))  
  
# len() function is used for a list  
print (len([110, 210, 130, 321]))  

Output:

14
4

Note:

Method overriding is a type of polymorphism in which a child class that extends the parent class can define any function defined in the parent class differently based on its own needs.

Method overloading is a type of polymorphism in which we can define a number of methods with the same name but with a different number of parameters, as well as parameters can be of various types. These methods can serve the same or another purpose.

Python does not support method overloading based on the number of parameters in a function.

Polymorphism with Class Methods:

Python employs two distinct class types in the same way. You must write a for loop that iterates through a tuple of objects in this case. The methods must then be called without regard for the class type of each object. We presume that these methods exist in each class.

Here’s an example of polymorphism in class:

class India():
     def capital(self):
       print("New Delhi")
 
     def language(self):
       print("Hindi and English")
 
class USA():
     def capital(self):
       print("Washington, D.C.")
 
     def language(self):
       print("English")
 
obj_ind = India()
obj_usa = USA()
for country in (obj_ind, obj_usa):
    country.capital()
    country.language()

Output:

New Delhi
Hindi and English
Washington, D.C.
English
Polymorphism with Inheritance :

In Python, polymorphism defines methods in the child class that have the same name as methods in the parent class. In inheritance, the child class inherits the parent class’s methods. It is also possible to change a method in a child class that has been inherited from the parent class.

For instance,

class Animal:
     def intro(self):
       print("There are different types of Animals")
 
     def action(self):
       print("Various animals can perform different actions")
 
class parrot(Animal):
     def action(self):
       print("Parrots can fly")
 
class shark(Animal):
     def action(self):
       print("sharks do not fly; they swim")
 
obj_animal = Animal()
obj_parr = parrot()
obj_shark = shark()
 
obj_animal.intro()
obj_animal.action()
 
obj_parr.intro()
obj_parr.action()
 
obj_shark.intro()
obj_shark.action()

Output:

There are different types of Animals
Various animals can perform different actions
There are different types of Animals
Parrots can fly
There are different types of Animals
sharks do not fly; they swim
Polymorphism with Functions:

It is also possible to create a function that can take any object, allowing for polymorphism. Consider the following example: create a function called “func()” that accepts an object called “obj”. Despite the name “obj,” any object that is instantiated will be able to call this function. Next, let’s give the function something to do with the ‘obj object that was passed to it. Let us refer to these three methods as websites(), topic(), and type() . They are defined in the classes ‘xyz’ and ‘PQR’. If we don’t already have instantiations of the ‘xyz” and ‘PQR classes, let us create them. Then, using the same function func, we can call their actions ().

def func(obj):  
       obj.websites()  
       obj.topic()  
      obj.type()  
  
obj_jtp = xyz()  
obj_pvl = PQR()  
  
func(obj_jtp)  
func(obj_pvl)  

Note: also read about Method Overriding in Python

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Reply

Your email address will not be published. Required fields are marked *