Python Polymorphism
About Python Polymorphism
The word polymorphism means many forms or states. In object-oriented programming, polymorphism refers to the usability of a function/method in different forms or states with different data types and number of arguments.
In Python, polymorphism refers to the way different object classes can share the same method name that act differently based on which object calls them.
There are several ways polymorphism can be implemented in Python:
Polymorphism with Inheritance
In Python, polymorphism is often achieved through inheritance by overriding the method of base class for a specific implementation. In other words, the functionality of the method of base class is replaced with new functionality in the derived class.
The overridden method in the derived class should have the same name and parameters as the method in the base class.
Output of the above example
Dog barks.
Animal makes sound.
Explanation of code
-
In the above example, Animal class is the base class.
def speak(self)is a method implemented in the base class. The 'speak' class return "Animal makes sound.". -
class Dog(Animal):: 'Dog' class is inherited from the 'Animal' class. Here 'Dog' class is a derived class. -
def speak(self): return "Dog barks.": 'Dog' class overrides the 'speak' method of base class which return "Dog barks.". The 'speak' method of 'Dog' class has the same name and parameters as the 'speak' method of 'Animal' class. -
print(dog.speak()): This calls the 'speak' method of 'Dog' class and prints the output of the method. The output is "Dog barks.". -
print(animalObj.speak()): This calls the 'speak' method of 'Animal' class and prints the output of the method. The output is "Animal makes sound." .
In the above example, the 'speak' method of 'Dog' class overrides the 'speak' method of 'Animal' class. So, when we call the 'speak' method using an instance of 'Dog' class, it executes the overridden method in the 'Dog' class and returns "Dog barks.". However, when we call the 'speak' method using an instance of 'Animal' class, it executes the original method in the 'Animal' class and returns "Animal makes sound.".
Polymorphism with Functions and Methods
In Python, polymorphism can be achieved in Python functions and methods by passing different types of arguments in the same function or method as demonstrated in the following example:
Output of the above example
8
Hello world!
Explanation of code
-
def add(x, y):: The line defines a function named 'add' with two parameters 'x' and 'y'. -
return x + y: The function returns the 'x+y'. -
print(add(5, 3)): Two integer numbers 5 and 3 are passed to the 'add' function as function arguments. The output is '8'. -
print(add("Hello ", "world!")): Two string type data "Hello " and "world!" are passed to the 'add' function as function arguments. The output is "Hello world!". Here the two strings are concatenated.Thus polymorphism is achieved with the function 'add' as the 'add' function can take different argument types.
Please note that Python itself doesn't support overloading functions with different numbers of arguments. However, we can achieve similar functionality by using default arguments or variable-length arguments in a single function definition.
Polymorphism with Duck typing
Python type system is flexible enough to support "duck typing". The "duck typing" is that "if it walks like a duck and quacks like a duck, then it must be a duck."
In other words, as long as an object has the required methods or attributes, it can be used in the same way, regardless of its actual type.
Output of the above example
Dog says Woof!
Cat says Meow!
Explanation of code
-
def get_animal_sound(animal):: The line defines a function named 'get_animal_sound' that takes an object 'animal' as argument. -
animal.make_sound(): The 'get_animal_sound' function calls the 'make_sound()' of 'animal' object. - Both Dog and Cat classes have a method name 'make_sound()'. So, the instances of Dog and Cat can be passed as arguments in the 'get_animal_sound' function.
-
get_animal_sound(Dog()): An instance of Dog class is passed as argument in the 'get_animal_sound' function. The output is "Dog says Woof!". -
get_animal_sound(Cat()): An instance of Cat class is passed as argument in the 'get_animal_sound' function. The output is "Cat says Meow!".
In the above example, the 'get_animal_sound' function can accept any object that has a 'make_sound()' method. The function doesn't care about the actual type of the object, as long as it has the required method. This is an example of polymorphism achieved through duck typing in Python.
Python Compiler For Live Practice
Practice the above Python code in the following Python Editor. Create more Python polymorphism examples in the following Python code editor and click Execute button to check the output. Output will be displayed in Python Code Output section.
Python Editor