The following code shows an Example of Polymorphism in Python.

In fact, this example shows how to perform polymorphism with inheritance. As a matter of fact, the classes FirstDerivedClass and the SecondDerivedClass are the subclasses of TheBaseClass. Moreover, all these three classes define the method Compute(). However, the implementation of this method is different in all of these three classes. The method call for Compute() method depends on the object which is used to call the method. Hence, it is the type of object that determines which of the three Compute() will be called.

# Polymorphism in Python
class TheBaseClass:
  def __init__(self, x, y):
  def Compute(self):
    return self.x*self.y

class FirstDerivedClass(TheBaseClass):
  def Compute(self):
    return (self.x+1)*(self.y+1)

class SecondDerivedClass(TheBaseClass):
  def Compute(self):
    return (self.x+2)*(self.y+2)

# Creating objects of these three classes
ob1=TheBaseClass(12, 19)
print("Result: "+str(ob1.Compute()))
ob2=FirstDerivedClass(12, 19)
print("Result: "+str(ob2.Compute()))
ob3=SecondDerivedClass(12, 19)
print("Result: "+str(ob3.Compute()))


Example of Polymorphism in Python to Show Methods Called at Runtime
Example of Polymorphism in Python to Show Methods Called at Runtime

Further Reading

Python Practice Exercise

Deep Learning Methods for Object Detection

Examples of OpenCV Library in Python

Examples of Tuples in Python

Python List Practice Exercise