Ways of implementing Polymorphism in Python
In programming, Polymorphism is a concept of Object-Oriented Programming. It enables using a single interface with the input of different data types, different classes or maybe for a different number of inputs.
Example: In this case, the function len() is polymorphic as it is taking a string as input in the first case and is taking a list as input in the second case.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
In Python, Polymorphism is a way of making a function accept objects of different classes if they behave similarly.
There are four ways of implementing Polymorphism in Python:
1. Duck Typing: Duck typing is a concept that says that the “type” of the object is a matter of concern only at runtime and you don’t need to explicitly mention the type of the object before you perform any kind of operation on that object, unlike normal typing where the suitability of an object is determined by its type.
In Python, we have the concept of Dynamic typing i.e we can mention the type of variable/object later. The idea is that you don’t need a type in order to invoke an existing method on an object if a method is defined on it, you can invoke it.
GeeksForGeeks is the best Platform for learning
2. Method Overloading: By default, Python does not support method overloading, but we can achieve it by modifying out methods.
Given a single function sum (), the number of parameters can be specified by you. This process of calling the same method in different ways is called method overloading.
1 8 6
3. Operator Overloading: Operator overloading in Python is the ability of a single operator to perform more than one operation based on the class (type) of operands. So, basically defining methods for operators is known as operator overloading. For e.g: To use the + operator with custom objects you need to define a method called __add__.
We know + operator is used for adding numbers and at the same time to concatenate strings. It is possible because the + operator is overloaded by both int class and str class. The operators are actually methods defined in respective classes.
So if you want to use the + operator to add two objects of some user-defined class then you will have to define that behavior yourself and inform Python about that.
TypeError: unsupported operand type(s) for +: 'Student' and 'Student'
So we can see that the + operator is not supported in a user-defined class. But we can do the same by overloading the + operator for our class.
4. Method Overriding: By using method overriding a class may “copy” another class, avoiding duplicated code, and at the same time enhance or customize a part of it. Method overriding is thus a part of the inheritance mechanism.
In Python, method overriding occurs by simply defining the child class in a method with the same name as a method in the parent class.
Hard work along with consistency can defeat Talent. Practice makes a man perfect