Join 36000+ teachers and students using TTIO.
Polymorphism is the ability of any data to be processed in more than one form. The word itself indicates the meaning as poly means many and morphism means types. Polymorphism is a very important concept in programming. It refers to the use of a single type entity (method, operator or object) to represent different types in different scenarios. Let's take an example:
We know that the +
operator is used extensively in Python programs. But, it does not have a single usage. For integer data types, +
operator is used to perform arithmetic addition operation.
num1 = 1
num2 = 2
print(num1+num2)
Hence, the above program outputs 3.
Similarly, for string data types, +
operator is used to perform concatenation.
str1 = "Python"
str2 = "Programming"
print(str1+" "+str2)
As a result, the above program outputs Python Programming. Here, we can see that a single operator +
has been used to carry out different operations for distinct data types. This is one of the most simple occurrences of polymorphism in Python.
https://pythonspot.com/polymorphism/
https://www.programiz.com/python-programming/polymorphism
https://www.geeksforgeeks.org/polymorphism-in-python/
Coded examples: https://www.edureka.co/blog/polymorphism-in-python/
www.teachyourselfpython.com