Polymorphism in Python

Опубликовано: 05 Октябрь 2024
на канале: Parag Dhawan
209
9

Polymorphism in Python
polymorphism means taking many forms.
Same entity gives different outputs at different times.
example: - function polymorphism

print(len("python programming"))
print(len([1,2,3,4,5]))
print(len({"one":1,"two":2}))
18
5
2
As the datatype changes output of len function also changes therefore polymorphic.
example :- operator polymorphism
num1 = 10
num2 = 20
num3 = num1 + num2
print(num3)
30
str1 = "object-Oriented"
str2 = "Concepts"
str3 = str1 + str2
print(str3)
object-OrientedConcepts
As the data type changes output of operator + changes. Same operator produces different output at different times hence polymorphic.
Example:- polymorphism while inheritance (method overriding)
class A:
def show(self):
print("show function from base class")

class B(A):
def show(self):
print("show function from class B which inherts A")

class C(A):
def show(self):
print("show function of class C which has inherited A ")

class D(C,B):
def show(self):
A.show(self)
B.show(self)
C.show(self)
print("show method of derived class")
d1 = D()
d1.show()
show funtion from base class
show function from class B which inherts A
show function of class C which has inherited A
show method of derived class
c1 = C()
c1.show()
show function of class C which has inherited A
Note:- python doesn't support method overloading i.e. functions with the same name and different prototypes.
example:- polymorphism with class Methods
class A:
def display(self):
print("Method in class A")

class B:
def display(self):
print("Method in class B")

obja = A()
objb = B()

for obj in (obja, objb):
obj.display()

Method in class A
Method in class B

=============================================================================
Link for Tutorial Series

Jupyter Notebook Tutorial Series:-
   • How To Open Jupyter Notebook in Windows  

Python Tutorial Series:-
   • Introduction to Python | Python Appli...  

Python Assignments and Objective Questions:-
   • Objective Questions Python - 1  

Tech. Videos By Parag Dhawan;-
   • Template Matching Using OpenCV (Pytho...  

Object-Oriented Programming in Python-
   • How to Create Class and Object in Python  

=============================================================================
Feel free to connect and ask your queries:-

Linkedin:-   / parag-dhawan  
Youtube:-    / paragdhawan  
Facebook Page:- http://fb.me/dhawanparag
Instagram: -   / paragdhawan  
Twitter:-   / dhawan_parag  
GitHub:- https://github.com/paragdhawan/
Facebook Profile:- https://www.facebook.com/profile.php?...

=============================================================================
Show your support by Subscribing to the channel:-
https://www.youtube.com/c/ParagDhawan...
=============================================================================
#ParagDhawan
#PythonCrashCourse
#Python
#PythonTutorialForBeginners
#PythonForDataScience
#PythonProgramming
#PythonProgrammingLanguage
#PythonTutorial
#PythonCode
#Python3
#ObjectOrientedProgramming
============================================================
How to Record Your Screen and make a tutorial video or demo video: -
   • How to Record Screen and Edit Video U...  
============================================================