Polymorphism in Python with Examples

Video Tutorial
FREE
Polymorphism in Python thumbnail
This video belongs to
Python Course for Beginners With Certification: Mastering the Essentials
16 modules
Certificate

Overview

Polymorphism,a fundamental concept in object-oriented programming, is the ability of one function to perform multiple functionalities. It can be implemented in different ways, including operator overloading, built-in functions, classes, and inheritance. In operator overloading, the same operator is used in different ways, such as addition and string concatenation.

Introduction

In the English language, the same word can be used in different sentences based on what we intend for it to mean.

For example- the word watch. It can be used in the following two ways-

  • “I bought a new watch today.”
  • “I really want to watch this movie tonight.”

Evidently enough, the word “watch” was used first as a noun and then a verb. Thus, the word is the same in both cases but the way we use it is different. Just like all other concepts that we came up with had real-life inspirations, so does the concept that we are going to discuss today - Polymorphism in Python!

What is Polymorphism in Python?

The literal meaning of Polymorphism is - having different forms. In programming, Polymorphism refers to a function having the same name but being used in different ways and different scenarios. This makes programming easier and more intuitive.

what is polymorphism in python

Polymorphism is one of the fundamental cornerstones of Object Oriented Programming. It is the ability of one function to display multiple functionalities all together. It basically creates a structure that can use many forms of objects.

Polymorphism in Python involves a child class inheriting all the methods from the parent class. This can be implemented in different ways which we will cover in detail in this article.

Polymorphism in Python Example

Let’s try to understand polymorphism and its implementation even better through this simple example -

The above code snippet is the simplest example. Over here, the “+” operator is being overloaded.

You must be wondering what is meant by being “overloaded”. Let me shed some light on that-
Operator overloading is the concept of using an operator beyond its pre-defined purpose.

So in our example, the “+” operator is performing addition in the first line while the second line is an example of string concatenation.

The output would make the difference clear

Here, you can see how the same operator was used in two different ways. Hence, operator overloading can be said to be an implementation of Polymorphism in Python.

The best example of Run-time polymorphism is method

Function of Polymorphism in Python

In Python, there are several functions that depict polymorphism by being compatible with multiple data types. The best example of this is the in-built len() function.

Let’s check out a small code snippet to see it in action -

Output-

From the above code snippet, we can clearly understand the len() function worked for all three data types- strings, lists and dictionaries.

function of polymorphism inpython

Class Polymorphism in Python

Polymorphism in Python can also be implemented through classes. Python allows classes to have different methods having the same name.

An example would make this concept a lot clearer -

class polymorphism in python

Now in this example, we have created two classes “Cat” and “Cow”. They’re different animals and make different sounds. So, the make_sound() function should produce two different outputs based on the objects we pass through them. In this case, we have created two objects “cat1” and “cow1”.

Now let’s see the final output that we will get -

As discussed, the make-sound() function is indeed producing two different outputs- “Meow” and “Moo”.

Polymorphism in Python with Inheritance

Polymorphism in Python lets us define the child class to have the same name methods as the parent class. On the other hand, inheritance in Python, the child class inherits all methods of the parent class.

Now, we may need to modify or re-define some of these. The process of making these changes specifically to the child class to ensure that they work seamlessly is known as Method Overriding.

Let’s look at a code snippet that implements Polymorphism with Inheritance in Python -

Note- You might have noticed that “pass” has been used within one of the functions.The pass statement in Python is used when you need it syntactically but do not wish for any code to be executed. It basically acts as a placeholder and prevents errors even when no code is written.

Output -

polymorphism in python with inheritance

I am sure you can make out from the output that the class “Circle” did not have a fact() method but even then a fact about it was printed to the console. I know what you are thinking!

This is happening, all thanks to Inheritance! Since the parent class “Shape” has a fact() method and the parent class is being inherited by the child class “Circle”, the fact() function is getting called and displayed.

Polymorphism with Class Methods

Python can use two different class types class times in the same way. This can be demonstrated through a simple example where we create two different classes and put them in a tuple.

If we now iterate this tuple of objects, the for loop will execute all the given methods for the first object in the tuple, then the second object in the tuple and so on.

Thus, in this way, we can say that Python would be calling or using these methods without knowing or caring about the class types of the objects. This is nothing but the example of Polymorphism in Python with class methods.

To make this concept even clearer, check out the following example -

Output - polymorphism with class methods

The functions mood() and sound() are same for both the classes yet they produce distinct outputs. Furthermore, we have iterated over the objects of the two classes “Cat” and “Dog” without worrying about the class types. Thus, now you know how to implement Polymorphism in Python with class methods.

Polymorphism with Function and Objects

Polymorphism in Python with functions and objects is a really interesting use-case. By defining a method and passing any object through it, we can implement Polymorphism with functions and objects in Python.

See this simple implementation -

In this code snippet, we have created two specific classes- Beans and Mango. Along with that, we have created a generic function that tells us the type and color of the object we pass. Mind you, since we have passed only “obj” through it, this obj can be any object. Next, we create two objects of the two above-mentioned classes.

Now let’s see what we get as the output -

polymorphism with function and-objects

From the output, it is clear, we were able to pass the two objects through the function really easily and also received accurate outputs. Hence, in this way we can make use of Polymorphism with classes and objects.

Conclusion

Congratulations, you have reached the end of this article!

To sum up what we learned so far, now you can confidently say-

  • You learned what Polymorphism means.
  • In particular, you now know what Polymorphism in Python is and the different ways to implement it.
  • You also have a fair idea about which situation requires which Polymorphism technique for your future coding sessions!