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).
Examples of user-defined polymorphic functions:
def add(x, y, z = 0):
return x + y+z
print(add(2, 3))
print(add(2, 3, 4))
output:
5
3
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 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.
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)
And 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.
class Animal():
def sound(self):
print("Animal makes sound.")
class Dog(Animal):
def sound1(self):
print("Dog barks")
a1=Dog() a1.sound()
And the output is:
Animal makes sound.
Congrats🎉, On Learning the Polymorphism in python with example programs.
Happy Learning :)