In Object Oriented programming approach the concept of polymorphism is about having a class objects in many forms. Polymorphism is of two types one is static polymorphism and the other is dynamic polymorphism.

Python do not have compile time polymorphism directly, that is because python programs do not use compilation specific to the machine in which they run ,instead the python programs are compiled to be run on a python virtual machine, and hence compilation is not specific to the machine, which makes python portable across platforms,more easier to learn and machine independent ,the picture below gives an overview of the components used to convert a python language program to a running code.

Operator Overloading

Operator overloading is about having different parameters on one operator, there are operators for arithmetic ,comparison,assignment and binary operation ,each operator for these operations can take different parameters and operate on it based on what type of parameter it is provided with,to perform this the users do not need to write the code every time, instead they can use the inbuilt methods in python,these methods are called magic methods .A few samples of the magic methods are listed below.

Function Overloading

Function overloading is a feature to allow a class to have methods with same name and different parameters,this feature is mainly used to avoid class duplicates. As python don’t have compile time binding ,overloading cannot be done directly, as in python if the function is defined with same name several times, at compile time only the last definition of the function will be taken into account , so overloading a function to have same name and work on different signature is possible only by defining a function to check for argument types inside the function,to make this easier for the users python provides a module called multimethod to help users to do polymorphism in easy way,this module allows users to use overloading feature by adding a decorator ‘@multimethod’ to the function definition ,hence this way the user will be able to define the function with same name and different signatures ,below is a sample using this module.

from multimethod import multimethod
class SuperMarket:

# Class Variable
name = “SmartBuy-State1”
location = “India”

# Instance variables
def __init__(self,rack1Count,rack2Count,rack3Count):
self.rack1Count = rack1Count
self.rack2Count = rack2Count
self.rack3Count = rack3Count

# Instance method
def totalItems(self):
return (self.rack1Count+self.rack2Count+self.rack3Count)

# Class method
@classmethod # This is a decorator
def storename(cls):
return cls.name

@multimethod
def printDiscount(self,x: int):
print(“The Customer has a shop card with number“,str(x))
answer = 10
print(“Discount is “,str(answer))

@multimethod
def printDiscount(self,x: str):
print(“The Customer is first time customer with no card number and name as “,str(x))
answer = 5
print(“Discount is “,str(answer))


# Instances of the class
Shop1 = SuperMarket(10,12,14)
Shop1.printDiscount(‘Customer1’)
Shop1.printDiscount(123)

AI Enthusiast