Polymorphism in Python (With Examples)

CodingCampus
3 min readNov 23, 2023

--

Polymorphism is a commonly used programming concept that relies on different forms to deliver different functionalities.

Python is an Object-oriented programming language that supports polymorphism. In this tutorial, we take a closer look at polymorphism in Python.

What is Polymorphism?

Polymorphism means taking different forms. In programming, it enables operators, functions, and methods to act differently when subjected to different conditions.

Polymorphism comes from the Greek words Poly(many) and morphism(forms).

As a programmer, you can use polymorphism to make programming more intuitive.

Polymorphism in Python

Polymorphism is deeply integrated with Python. It starts with operators. Let’s look at an example.

Operator addition polymorphism

The + operator lets you add numbers and strings and works differently when used with other data structures.

If you add two numbers, it will give you a numerical result. However, the + operator with polymorphism enables to add two strings.

a = 1
b = 2
print(a+b)a_string = "Nitish"
b_string = "Singh"
print(a_string+ " " + b_string)

The output is:

3
Nitish Singh

The + operator's polymorphism capability enables it to identify the inputs and perform operations accordingly.

Python function polymorphism

Next comes functions that support polymorphism. Python built-in functions provide polymorphism, such as the len() function.

Let’s see it in action.

print(len("Nitish Singh"))
print(len([13,15,29]))print(len({"Name": "John", "Address": "Nepal"})) #dictionary example

The output is:

12
3
2

The len() function works differently when passed different input. So, it works with a string, a list and a dictionary and can identify the data type and generate output accordingly.

Polymorphism with class methods

You are also free to create your own functions that show polymorphism. In this example, we will create class methods to showcase polymorphism.

#Class Polymorphism
class Liquid:
def __init__(self, name, formula):
self.name = name
self.formula = formula
def info(self):
print(f"I am Liquid Form. I am {self.name}. and my formula is {self.formula}")
def property(self):
print("I am clear and light form")
class Solid:
def __init__(self, name, formula):
self.name = name
self.formula = formula
def info(self):
print(f"I am Solid Form. I am {self.name}. and my formula is {self.formula}")
def property(self):
print("I can be transparent or clear or completely opaque")
liquid_1 = Liquid("Water", "H20")solid_1 = Solid("Ice", "H20")for material in (liquid_1,solid_1):
material.info()
material.property()

And the output is:

I am Liquid Form. I am Water. and my formula is H20
I am clear and light form
I am Liquid Form. I am Ice. and my formula is H20
I can be transparent or clear or complete opaque

Here, we create two classes: Liquid and Solid. Here, we define info() and property() for both of them. These functions work differently depending on which class object you call. You can also add class GAS and class PLASMA if you like experimenting.

Polymorphism with Objects and Functions

Functions that take objects can also exhibit polymorphism. Let’s look at an example below.

#Functions with object as parameters #polymorphism
class PlayStation():
def type(self):
print("PlayStation 5")
def company(self):
print("Sony")
class XBOX():
def type(self):
print("XBOX Series S")
def company(self):
print("Microsoft")
def func(obj):
obj.type()
obj.company()
obj_PlayStation = PlayStation()
obj_XBOX = XBOX()
func(obj_PlayStation)
func(obj_XBOX)

The output is:

PlayStation 5
Sony
XBOX Series S
Microsoft

Polymorphism with Inheritance

In Object-oriented programming, class inheritance lets you inherit the methods and attributes of a parent class to a child class. The parent class is also known as base class, whereas the derived subclass is known as derived or child class.

Child classes uses method overriding polymorphism to implement inheritance. Let’s look at the code below.

#Polymorphism with inheritance #method overloading
class F1():
def run(self):
print("Win")
class F2(F1):
def run(self):
print("2nd place - F2 cars are slower than F1 cars")
class F3(F1):
def run(self):
print("3rd place - F3 cars are slower than F2 and F1 cars")
obj_F1 = F1()
obj_F2 = F2()
obj_F3 = F3()
obj_F1.run()
obj_F2.run()
obj_F3.run()

And the output is:

Win
2nd place - F2 cars are slower than F1 cars
3rd place - F3 cars are slower than F2 and F1 cars

Here, we first defined our F1 class and then passed it as a parameter in the F2 class. Each time we called the run() function on objects of F1, F2 and F3 class, the inheritance and method overriding took place. This is a perfect example of polymorphism with inheritance and method overriding.

--

--