Polymorphism in Python

106

What is Polymorphism?

The basic meaning of polymorphism is the same thing existing in different forms.

Similarly, in the case of programming polymorphism means the same function has different versions that may differ in arguments passed to them. The working of these functions will also differ and different versions of functions will run according to which one is needed according to the given number of arguments.

Polymorphism in Python with examples

We will learn this with the help of some examples –

 Example of inbuilt polymorphic function in python

print(len("yuvayana"))

print(len([15, 25, 35]))

Output –

8
3

Example of user-defined polymorphic function in Python

def sum(x, y, z=0):
    return x + y + z
print(sum(1, 2))
print(sum(8, 3, 5))

Output –

3

16

Polymorphism in Class Methods –

We can have two different functions with the same methods defined in them and the same structure.

However, we do not create a common superclass or linked the classes together in any way.

Even then, we can pack these two different objects into a tuple and iterate through it using a common variable x. It is possible due to polymorphism.

Polymorphism and Inheritance

When we inherit a class, the methods defined within the class are also carried on to the child class. But, we can redefine these methods in the child class according to the need of the users.

Polymorphism allows us to access these overridden methods and attributes that have the same name as the parent class.

We will understand this with the help of the example given below –

from math import pi

class Shape:
    def __init__(self, name):
        self.name = name

    def area_of_shape(self):
        pass

    def type(self):
        return "I am a two-dimensional shape."

    def __str__(self):
        return self.name


class Square(Shape):
    def __init__(self, length):
        super().__init__("Square")
        self.length = length

    def area_of_shape(self):
        return self.length**2

    def type(self):
        return "Squares have each angle equal to 90 degrees."


class Circle(Shape):
    def __init__(self, radius):
        super().__init__("Circle")
        self.radius = radius

    def area_of_shape(self):
        return pi*self.radius**2


a = Square(4)
b = Circle(7)
print(a)
print(b)
print(b.type())
print(a.type())
print(a.area_of_shape())
print(b.area_of_shape())

Output –

Square
Circle
I am a two-dimensional shape.
Squares have each angle equal to 90 degrees.
16
153.93804002589985

Apart from this, we need to understand that in Python, Methods Overloading which is creating multiple methods with the same name is not possible in Python.

The problem with method overloading in Python is that we may overload the methods but can only use the latest defined method.

So, in this article, we learned about Polymorphism in Python. In any language, Polymorphism is one of the most important concepts of object-oriented programming.

Try Free Python Online Test

Previous articleOrderedDict In Python
Next articleComplexity Classes
Harshit brijwasi Yuvayana
I am Harshit Brijwasi. I am an engineer and a Technical Content writer. I am here to share my knowledge about technical topics and help researchers with them.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.