Polymorphism in Python With Examples

In this post we’ll see the usage of OOPS concept Polymorphism in Python.

What is Polymorphism

Polymorphism is a Greek word where poly means “many” and morph means “change from one form to another”. In object oriented terms it relates to the same object reference taking many forms (assigned different types), a method with the same name having more than one implementations, an operator behaving differently for different operands.

Polymorphism in Python

In an object oriented language you may see use of Polymorphism in one of the following ways

  • Method overloading, also known as compile time Polymorphism
  • Method overriding, also known as run time Polymorphism
  • Operator overloading

In Python you will find support for Polymorphism through Method overriding and Operator overloading. Python doesn’t support method overloading in its traditional sense though. Also when discussing Polymorphism in Python you should also get to know about duck typing in Python. So let’s see some examples.

Compile time polymorphism (Method Overloading)

Method overloading means having multiple methods with the same name in a class. These overloaded methods differ in types or number of arguments passed.
Python doesn’t support compile time polymorphism or method overloading. If there are multiple methods with the same name in a class only the last defined method is recognized. Calling any other overloaded method results in an error.

Read more about Method Overloading in Python in this post- Method Overloading in Python With Examples

Runtime polymorphism (Method Overriding)

In case of inheritance child class inherits all the properties and methods of parent class. If there is a method in a child class that has the same name and same number of arguments as in parent class then this process is called method overriding where the child class method is said to be overriding the parent class method.

Read more about Method Overriding in Python in this post- Method Overriding in Python With Examples

When you call the overridden method with parent class object, method of the parent class is executed. When same method is called with child class object, method of the child class is executed. So the appropriate overridden method is called based on the object type, which is an example of run time polymorphism.

Consider the following class hierarchy where super class Animal has two methods info() and make_sound(). There are two child classes Duck and Dog overriding both of the methods.

class Animal:
    def info(self):
        print('I am an animal')

    def make_sound(self):
        pass


class Duck(Animal):
    def info(self):
        print('I am a Duck')

    def make_sound(self):
        print('Quack Quack')


class Dog(Animal):
    def info(self):
        print('I am a Dog')

    def make_sound(self):
        print('Bow Wow')


d = Duck()
d.info()
d.make_sound()
d = Dog()
d.info()
d.make_sound()

Output

I am a Duck
Quack Quack
I am a Dog
Bow Wow

When d refers to an object of Duck it calls the method of Duck class, when d refers to an object of Dog class it calls the method of that class.

Polymorphism in Python through operator overloading

Operator overloading is also an example of polymorphism where the same operator performs different operations based on the type of operands. Python supports operator overloading.

For example ‘+’ operator-

  • When used with numbers it performs addition operation.
  • When used with two strings concatenate those strings
  • When used with lists merge those lists
# + operator with integers- Addition
print(3 + 4)
# + operator with Strings- Concatenation
print("Operator " + "Overloading")
a = [10, 11, 12]
b = [100, 200, 300]
# + operator with Lists- Merging
print(a + b)

Output

7
Operator Overloading
[10, 11, 12, 100, 200, 300]

Duck typing and Polymorphism

Python follows the duck typing philosophy which states “If it walks like a duck, and it quacks like a duck, then it must be a duck”.

In a dynamic language like Python it means that you don’t worry about the type or the class of an object. You can perform the required action with the object or not is more important.

Because of this duck typing principle followed by Python it is possible to create a function that can take any object, allowing for polymorphism. As long as the passed object has the called method it can be called. Let’s clear it with an example where we have two classes with a method make_sound(). There is a function invoke which takes any object as an argument and calls the make_sound() method on that object. When invoke function is called with an object of class Duck make_sound() method of the Duck class is called, when it is called with an object of class Dog make_sound() method of the Dog class is called.

class Duck:
    def make_sound(self):
        print("Quack Quack")


class Dog:
    def make_sound(self):
        print('Bow Wow')


def invoke(obj):
    obj.make_sound()


d = Duck()
invoke(d)
d = Dog()
invoke(d)

Output

Quack Quack
Bow Wow

Related Posts

That’s all for the topic Polymorphism in Python With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

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

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