PYTHON POLYMORPHISM
Polymorphism is one of the OOP(object-oriented programming) concepts. Polymorphism in Python is the ability of an object to take many forms. The word โpolymorphismโ means โmany formsโ, and in programming, it refers to methods/functions/operators with the same name that can be executed on many objects or classes.
Function Polymorphism in Python
In Python Polymorphism, a method can process objects differently depending on the class type or data type.
To understand the concept in better way lets take an example of a Python built-in method that can be used on different objects is the len()ย function.
The Python built-in function len() calculates the length of an object depending upon its type. If an object is a string, it returns the count of characters, and If an object is a list, it returns the count of items in a list. and if an object is a dictionary, it returns the count of keys.
String
For strings len() returns the number of characters:
Python code
x = "Apple"
print(len(x))
Output
5
Tuple
For tuples len() returns the number of items in the tuple:
Python code
fruits = ("apple", "banana", "cherry")
print(len(fruits))
Output
3
Dictionary
For dictionaries len() returns the number of key/value pairs in the dictionary:
Python code
fruits = {
"red": "Apple",
"yellow": "Banana",
"Brown": "Kiwi"
}
print(len(fruits))
Output
3
Class Polymorphism in Python
Polymorphism in Python classes can be applied by having multiple classes with the same method name.
For example, say we have three classes: Bus, Boat, and Plane, and they all have a method called move():
Python code to show different classes with the same method:
class Bus:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def move(self):
print("Drive!")
class Boat:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def move(self):
print("Sail!")
class Plane:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def move(self):
print("Fly!")
bus1 = Bus("Tata", "Winger") #Create a Bus class
boat1 = Boat("Ibiza", "Touring 20") #Create a Boat class
plane1 = Plane("Boeing", "747") #Create a Plane class
for x in (bus1, boat1, plane1):
x.move()
Output
Drive!
Sail!
Fly!
Inheritance Class Polymorphism in Python
Inheritance along with polymorphism can be used in classes in Python.
In inheritance, child class inherits the attributes and methods of a parent class. The existing class is called a base class or parent class, and the new class is called a subclass or child class or derived class.
Using method overriding polymorphism allows us to define methods in the child class that have the same name as the methods in the parent class. This process of re-implementing the inherited method in the child class is known as Method Overriding.
Python code to create a class called Vehicle and make Bus, Boat, Plane child classes of Vehicle
class Vehicle:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def move(self):
print("Move!")
class Bus(Vehicle):
pass
class Boat(Vehicle):
def move(self):
print("Sail!")
class Plane(Vehicle):
def move(self):
print("Fly!")
bus1 = Bus("Tata", "Winger") #Create a Bus object
boat1 = Boat("Ibiza", "Touring 20") #Create a Boat object
plane1 = Plane("Boeing", "747") #Create a Plane object
for x in (bus1, boat1, plane1):
print(x.brand)
print(x.model)
x.move()
output
Tata
Winger
Move!
Ibiza
Touring 20
Sail!
Boeing
747
Fly!
In the example above we can see that the Bus class(child class) is not having any content or it is empty, but it inherits the brand, model, and move() from the Vehicle(parent class).
The Boat and Plane (Child Classes) also inherit brand, model, and move() from Vehicle(Parent Class), but they both override the move() method.
Because of polymorphism, we are able to execute the same method for all classes.
Leave a Reply