A Quick Guide on Polymorphism in Python

Filed Under: Python
Polymorphism In Python

Polymorphism in Python is the ability of anything to take many forms which allows us to perform the same action in many different ways. Suppose a child behaves like a student when he is in school and he behaves like a customer in the market. In his house, he acts like a son and so on. Look at the image below.

Polymorphism Real Life
Polymorphism Real Life

In OOPS, it allows using a particular object of a particular class in a similar fashion as if it was a different object referring to altogether a different class. For instance, a function draw can be used to draw any shape, be it rectangle, square, circle, or any other shape.

Recommended Read: Python Inheritance Example

Polymorphism on len function

The len function can calculate the length of a list as well as the number of characters in a string. Look at the code below.

l = ["Journal", "Dev"]
s = "JournalDev"

print("Length of List : ",len(l))
print("Length of String : ",len(s))

Output –

Length of List :  2
Length of String :  10

Implementing Polymorphism in Python with Class

Python can use different types of classes, in the same way by creating a loop that iterates through a tuple of class objects. One can call the functions without having a look at the type of class to which the class object belongs to.

class Dog(): 
    def age(self): 
        print("This function determines the age of Dog.") 
    def Breed(self): 
        print("This function determines the Breed of Dog.") 
class Cat(): 
    def age(self): 
        print("This function determines the age of Cat.") 
    def Breed(self): 
        print("This function determines the Breed of Cat.") 
o1 = Cat() 
o2 = Dog()
for type in (o1, o2):
    type.age() 
    type.Breed() 

Output –

This function determines the age of Cat.
This function determines the Breed of Cat.
This function determines the age of Dog.
This function determines the Breed of Dog.

Method and Operator Overloading

Method Overloading: Two or more methods within the same class can share a separate number of parameters and have the same name. Also, It is also possible to overpower constructors in Python.

Operator Overloading: Giving an operator an extended meaning beyond its predefined operations. For Example The + operator can add two integers as well as two strings.

Advantage and Disadvantage of polymorphism

In OOPs, Polymorphism is one of the few concepts that actually matters. Learning this has multiple benefits when you go into advanced programming. Let’s look at 2 advantages and disadvantages of polymorphism in Python.

Advantages

It allows coders to reuse, evaluate, and execute the code which is written only once. In certain aspects, they can repeat multiple times. It tends to reduce the pairing of multiple functionalities.

Disadvantages

One of the key disadvantages is that polymorphism can complicate the code for programmers in a number of ways. It will take run time and will lead to the performance of the code. The readability of the program is also reduced with polymorphism.

Conclusion

Polymorphism is an object‘s capability to accept multiple forms. This tutorial covers the basics of the same in the Python programming language. I hope you are clear with the concept now.

Thank you for reading!

Recommended Read: Python Multiple Inheritance

close
Generic selectors
Exact matches only
Search in title
Search in content