SlideShare a Scribd company logo
Polymorphism in Python - Detailed
Explanation
• An overview of polymorphism with examples
and real-life applications.
What is Polymorphism?
• • Derived from Greek: 'Poly' (many) + 'Morph'
(forms)
• • Ability of an object to take multiple forms
• • Same function/method behaves differently
based on the object
Why is Polymorphism Important?
• • Flexibility and code reusability
• • Generalization: One function works for
multiple object types
• • Enhances readability and maintenance
Types of Polymorphism in Python
• 1. Duck Typing
• 2. Method Overriding (Runtime
Polymorphism)
• 3. Operator Overloading
Duck Typing Example
•Duck Typing allows objects to be used
interchangeably as long as they
•implement the required behavior (i.e., they have
the expected methods), regardless of the class they
belong to.
•This is why Python doesn't care about the
"type" of the object, but rather if it can perform a
certain action.
Duck Typing Example
class Cat:
def sound(self):
return 'Meow'
class Dog:
def sound(self):
return 'Bark'
def animal_sound(animal):
print(animal.sound())
animal_sound(Cat()) # Meow
animal_sound(Dog()) # Bark
Duck Typing Example
Write a program to illustrate duck typing in
Python using Banking as a scenario
//use google collab
if hasattr
operator overloading
Method Overriding Example
class Shape:
def area(self):
return 0
class Rectangle(Shape):
def __init__(self, length, width):
self.length = length
self.width = width
def area(self):
return self.length * self.width
shape = Shape()
rectangle = Rectangle(5, 10)
print(shape.area()) # 0
print(rectangle.area()) # 50
Operator Overloading
Operator Overloading (also called operator ad-hoc
polymorphism) allows you to define or change the
behavior of built-in operators (like +, -, *, etc.) for custom
classes.
•Operator Overloading allows you to use operators (such
as +, -, *, etc.) to work with objects of your class, just like
they work with primitive data types like integers and strings.
Operator Overloading
What is Operator Overloading?
In Python, operator overloading allows you to redefine the
meaning of standard operators (+, -, *, etc.) when they are
used with objects of your own class. This means you can
customize how an operator behaves when applied to your
objects.
Operator Overloading
Operator Overloading Example
class Book:
def __init__(self, pages):
self.pages = pages
def __add__(self, other):
return self.pages + other.pages
book1 = Book(150)
book2 = Book(200)
print(book1 + book2) # 350
We use operator overloading when we want to
make custom classes behave more like built-in
types, especially when using operators such as
+, -, *, ==, etc.
When Do We Use Operator Overloading?
When Do We Use Operator Overloading?
When Do We Use Operator Overloading?
When Do We Use Operator Overloading?
Real-Life Example - Payment System
class Payment:
def pay(self):
pass
class CreditCard(Payment):
def pay(self):
return 'Paid using Credit Card'
class Paypal(Payment):
def pay(self):
return 'Paid using Paypal'
def make_payment(payment_method):
print(payment_method.pay())
make_payment(CreditCard())
make_payment(Paypal())
Benefits of Polymorphism
• Code Reusability: Same interface for multiple
object types
• Extensibility: Easy to add new classes
• Maintenance: Easier to maintain and scale
code
• Cleaner Code: Reduces complex if-else
structures
Ad

More Related Content

Similar to Polymorphism_in_Python_Programming_Language (20)

java poly ppt.pptx
java poly ppt.pptxjava poly ppt.pptx
java poly ppt.pptx
sukhpreetsingh295239
 
Python presentation of Government Engineering College Aurangabad, Bihar
Python presentation of Government Engineering College Aurangabad, BiharPython presentation of Government Engineering College Aurangabad, Bihar
Python presentation of Government Engineering College Aurangabad, Bihar
UttamKumar617567
 
PYTHON PPT.pptx python is very useful for day to day life
PYTHON PPT.pptx python is very useful for day to day lifePYTHON PPT.pptx python is very useful for day to day life
PYTHON PPT.pptx python is very useful for day to day life
NaitikSingh33
 
Python introduction
Python introductionPython introduction
Python introduction
Roger Xia
 
PYTHON OBJECT-ORIENTED PROGRAMMING.pptx
PYTHON  OBJECT-ORIENTED PROGRAMMING.pptxPYTHON  OBJECT-ORIENTED PROGRAMMING.pptx
PYTHON OBJECT-ORIENTED PROGRAMMING.pptx
hpearl130
 
OOP_UnitIII.pdf
OOP_UnitIII.pdfOOP_UnitIII.pdf
OOP_UnitIII.pdf
SuyogSabale1
 
Lecture on Lecture on Python programming OP conceptsPolymorphism.pdf
Lecture on Lecture on Python programming OP conceptsPolymorphism.pdfLecture on Lecture on Python programming OP conceptsPolymorphism.pdf
Lecture on Lecture on Python programming OP conceptsPolymorphism.pdf
waqaskhan428678
 
Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
harsh kothari
 
Polymorphism and its types
Polymorphism and its typesPolymorphism and its types
Polymorphism and its types
Suraj Bora
 
polymorphism and virtual function
polymorphism and virtual functionpolymorphism and virtual function
polymorphism and virtual function
Bhanuprataparya
 
From Java to Python: beating the Stockholm syndrome
From Java to Python: beating the Stockholm syndromeFrom Java to Python: beating the Stockholm syndrome
From Java to Python: beating the Stockholm syndrome
Javier Arias Losada
 
Introduction to oop
Introduction to oopIntroduction to oop
Introduction to oop
colleges
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4th
Connex
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answers
kavinilavuG
 
Polymorphism.Difference between Inheritance & Polymorphism
Polymorphism.Difference between Inheritance & PolymorphismPolymorphism.Difference between Inheritance & Polymorphism
Polymorphism.Difference between Inheritance & Polymorphism
huzaifaakram12
 
oops.pptx
oops.pptxoops.pptx
oops.pptx
ASHWINAIT2021
 
Introduction to Object Oriented Programming in Python.pptx
Introduction to Object Oriented Programming in Python.pptxIntroduction to Object Oriented Programming in Python.pptx
Introduction to Object Oriented Programming in Python.pptx
eduardocehenmu
 
Polymorphism topic power point presentation li.pptx
Polymorphism topic power point presentation li.pptxPolymorphism topic power point presentation li.pptx
Polymorphism topic power point presentation li.pptx
MalligaarjunanN
 
full defination of final opp.pptx
full defination of final opp.pptxfull defination of final opp.pptx
full defination of final opp.pptx
rayanbabur
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answers
RojaPriya
 
Python presentation of Government Engineering College Aurangabad, Bihar
Python presentation of Government Engineering College Aurangabad, BiharPython presentation of Government Engineering College Aurangabad, Bihar
Python presentation of Government Engineering College Aurangabad, Bihar
UttamKumar617567
 
PYTHON PPT.pptx python is very useful for day to day life
PYTHON PPT.pptx python is very useful for day to day lifePYTHON PPT.pptx python is very useful for day to day life
PYTHON PPT.pptx python is very useful for day to day life
NaitikSingh33
 
Python introduction
Python introductionPython introduction
Python introduction
Roger Xia
 
PYTHON OBJECT-ORIENTED PROGRAMMING.pptx
PYTHON  OBJECT-ORIENTED PROGRAMMING.pptxPYTHON  OBJECT-ORIENTED PROGRAMMING.pptx
PYTHON OBJECT-ORIENTED PROGRAMMING.pptx
hpearl130
 
Lecture on Lecture on Python programming OP conceptsPolymorphism.pdf
Lecture on Lecture on Python programming OP conceptsPolymorphism.pdfLecture on Lecture on Python programming OP conceptsPolymorphism.pdf
Lecture on Lecture on Python programming OP conceptsPolymorphism.pdf
waqaskhan428678
 
Polymorphism and its types
Polymorphism and its typesPolymorphism and its types
Polymorphism and its types
Suraj Bora
 
polymorphism and virtual function
polymorphism and virtual functionpolymorphism and virtual function
polymorphism and virtual function
Bhanuprataparya
 
From Java to Python: beating the Stockholm syndrome
From Java to Python: beating the Stockholm syndromeFrom Java to Python: beating the Stockholm syndrome
From Java to Python: beating the Stockholm syndrome
Javier Arias Losada
 
Introduction to oop
Introduction to oopIntroduction to oop
Introduction to oop
colleges
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4th
Connex
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answers
kavinilavuG
 
Polymorphism.Difference between Inheritance & Polymorphism
Polymorphism.Difference between Inheritance & PolymorphismPolymorphism.Difference between Inheritance & Polymorphism
Polymorphism.Difference between Inheritance & Polymorphism
huzaifaakram12
 
Introduction to Object Oriented Programming in Python.pptx
Introduction to Object Oriented Programming in Python.pptxIntroduction to Object Oriented Programming in Python.pptx
Introduction to Object Oriented Programming in Python.pptx
eduardocehenmu
 
Polymorphism topic power point presentation li.pptx
Polymorphism topic power point presentation li.pptxPolymorphism topic power point presentation li.pptx
Polymorphism topic power point presentation li.pptx
MalligaarjunanN
 
full defination of final opp.pptx
full defination of final opp.pptxfull defination of final opp.pptx
full defination of final opp.pptx
rayanbabur
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answers
RojaPriya
 

More from abigailjudith8 (10)

Inheritance_in_OOP_using Python Programming
Inheritance_in_OOP_using Python ProgrammingInheritance_in_OOP_using Python Programming
Inheritance_in_OOP_using Python Programming
abigailjudith8
 
Encapsulation_Python_Programming_Language
Encapsulation_Python_Programming_LanguageEncapsulation_Python_Programming_Language
Encapsulation_Python_Programming_Language
abigailjudith8
 
Application and Data security and Privacy.pptx
Application and Data security and Privacy.pptxApplication and Data security and Privacy.pptx
Application and Data security and Privacy.pptx
abigailjudith8
 
Cyber Hackathon Media Campaign Proposal (1).pptx
Cyber Hackathon Media Campaign Proposal (1).pptxCyber Hackathon Media Campaign Proposal (1).pptx
Cyber Hackathon Media Campaign Proposal (1).pptx
abigailjudith8
 
SVM FOR GRADE 11 pearson Btec 3rd level.ppt
SVM FOR GRADE 11 pearson Btec 3rd level.pptSVM FOR GRADE 11 pearson Btec 3rd level.ppt
SVM FOR GRADE 11 pearson Btec 3rd level.ppt
abigailjudith8
 
MACHINE LEARNING INTRODUCTION FOR BEGINNERS
MACHINE LEARNING INTRODUCTION FOR BEGINNERSMACHINE LEARNING INTRODUCTION FOR BEGINNERS
MACHINE LEARNING INTRODUCTION FOR BEGINNERS
abigailjudith8
 
lect1-introductiontoprogramminglanguages-130130013038-phpapp02.ppt
lect1-introductiontoprogramminglanguages-130130013038-phpapp02.pptlect1-introductiontoprogramminglanguages-130130013038-phpapp02.ppt
lect1-introductiontoprogramminglanguages-130130013038-phpapp02.ppt
abigailjudith8
 
SVM introduction for machine learning engineers
SVM introduction for machine learning engineersSVM introduction for machine learning engineers
SVM introduction for machine learning engineers
abigailjudith8
 
Big Data for Pearson Btec Higher level 3.ppt
Big Data for Pearson Btec Higher level 3.pptBig Data for Pearson Btec Higher level 3.ppt
Big Data for Pearson Btec Higher level 3.ppt
abigailjudith8
 
INTRODUCTION TO PROGRAMMING and Python.pptx
INTRODUCTION TO PROGRAMMING and Python.pptxINTRODUCTION TO PROGRAMMING and Python.pptx
INTRODUCTION TO PROGRAMMING and Python.pptx
abigailjudith8
 
Inheritance_in_OOP_using Python Programming
Inheritance_in_OOP_using Python ProgrammingInheritance_in_OOP_using Python Programming
Inheritance_in_OOP_using Python Programming
abigailjudith8
 
Encapsulation_Python_Programming_Language
Encapsulation_Python_Programming_LanguageEncapsulation_Python_Programming_Language
Encapsulation_Python_Programming_Language
abigailjudith8
 
Application and Data security and Privacy.pptx
Application and Data security and Privacy.pptxApplication and Data security and Privacy.pptx
Application and Data security and Privacy.pptx
abigailjudith8
 
Cyber Hackathon Media Campaign Proposal (1).pptx
Cyber Hackathon Media Campaign Proposal (1).pptxCyber Hackathon Media Campaign Proposal (1).pptx
Cyber Hackathon Media Campaign Proposal (1).pptx
abigailjudith8
 
SVM FOR GRADE 11 pearson Btec 3rd level.ppt
SVM FOR GRADE 11 pearson Btec 3rd level.pptSVM FOR GRADE 11 pearson Btec 3rd level.ppt
SVM FOR GRADE 11 pearson Btec 3rd level.ppt
abigailjudith8
 
MACHINE LEARNING INTRODUCTION FOR BEGINNERS
MACHINE LEARNING INTRODUCTION FOR BEGINNERSMACHINE LEARNING INTRODUCTION FOR BEGINNERS
MACHINE LEARNING INTRODUCTION FOR BEGINNERS
abigailjudith8
 
lect1-introductiontoprogramminglanguages-130130013038-phpapp02.ppt
lect1-introductiontoprogramminglanguages-130130013038-phpapp02.pptlect1-introductiontoprogramminglanguages-130130013038-phpapp02.ppt
lect1-introductiontoprogramminglanguages-130130013038-phpapp02.ppt
abigailjudith8
 
SVM introduction for machine learning engineers
SVM introduction for machine learning engineersSVM introduction for machine learning engineers
SVM introduction for machine learning engineers
abigailjudith8
 
Big Data for Pearson Btec Higher level 3.ppt
Big Data for Pearson Btec Higher level 3.pptBig Data for Pearson Btec Higher level 3.ppt
Big Data for Pearson Btec Higher level 3.ppt
abigailjudith8
 
INTRODUCTION TO PROGRAMMING and Python.pptx
INTRODUCTION TO PROGRAMMING and Python.pptxINTRODUCTION TO PROGRAMMING and Python.pptx
INTRODUCTION TO PROGRAMMING and Python.pptx
abigailjudith8
 
Ad

Recently uploaded (20)

Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
Build 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHSBuild 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHS
TECH EHS Solution
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
TrsLabs Consultants - DeFi, WEb3, Token Listing
TrsLabs Consultants - DeFi, WEb3, Token ListingTrsLabs Consultants - DeFi, WEb3, Token Listing
TrsLabs Consultants - DeFi, WEb3, Token Listing
Trs Labs
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
 
Vaibhav Gupta BAML: AI work flows without Hallucinations
Vaibhav Gupta BAML: AI work flows without HallucinationsVaibhav Gupta BAML: AI work flows without Hallucinations
Vaibhav Gupta BAML: AI work flows without Hallucinations
john409870
 
Are Cloud PBX Providers in India Reliable for Small Businesses (1).pdf
Are Cloud PBX Providers in India Reliable for Small Businesses (1).pdfAre Cloud PBX Providers in India Reliable for Small Businesses (1).pdf
Are Cloud PBX Providers in India Reliable for Small Businesses (1).pdf
Telecoms Supermarket
 
MINDCTI revenue release Quarter 1 2025 PR
MINDCTI revenue release Quarter 1 2025 PRMINDCTI revenue release Quarter 1 2025 PR
MINDCTI revenue release Quarter 1 2025 PR
MIND CTI
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
Build 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHSBuild 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHS
TECH EHS Solution
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
TrsLabs Consultants - DeFi, WEb3, Token Listing
TrsLabs Consultants - DeFi, WEb3, Token ListingTrsLabs Consultants - DeFi, WEb3, Token Listing
TrsLabs Consultants - DeFi, WEb3, Token Listing
Trs Labs
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
 
Vaibhav Gupta BAML: AI work flows without Hallucinations
Vaibhav Gupta BAML: AI work flows without HallucinationsVaibhav Gupta BAML: AI work flows without Hallucinations
Vaibhav Gupta BAML: AI work flows without Hallucinations
john409870
 
Are Cloud PBX Providers in India Reliable for Small Businesses (1).pdf
Are Cloud PBX Providers in India Reliable for Small Businesses (1).pdfAre Cloud PBX Providers in India Reliable for Small Businesses (1).pdf
Are Cloud PBX Providers in India Reliable for Small Businesses (1).pdf
Telecoms Supermarket
 
MINDCTI revenue release Quarter 1 2025 PR
MINDCTI revenue release Quarter 1 2025 PRMINDCTI revenue release Quarter 1 2025 PR
MINDCTI revenue release Quarter 1 2025 PR
MIND CTI
 
Ad

Polymorphism_in_Python_Programming_Language

  • 1. Polymorphism in Python - Detailed Explanation • An overview of polymorphism with examples and real-life applications.
  • 2. What is Polymorphism? • • Derived from Greek: 'Poly' (many) + 'Morph' (forms) • • Ability of an object to take multiple forms • • Same function/method behaves differently based on the object
  • 3. Why is Polymorphism Important? • • Flexibility and code reusability • • Generalization: One function works for multiple object types • • Enhances readability and maintenance
  • 4. Types of Polymorphism in Python • 1. Duck Typing • 2. Method Overriding (Runtime Polymorphism) • 3. Operator Overloading
  • 5. Duck Typing Example •Duck Typing allows objects to be used interchangeably as long as they •implement the required behavior (i.e., they have the expected methods), regardless of the class they belong to. •This is why Python doesn't care about the "type" of the object, but rather if it can perform a certain action.
  • 6. Duck Typing Example class Cat: def sound(self): return 'Meow' class Dog: def sound(self): return 'Bark' def animal_sound(animal): print(animal.sound()) animal_sound(Cat()) # Meow animal_sound(Dog()) # Bark
  • 7. Duck Typing Example Write a program to illustrate duck typing in Python using Banking as a scenario //use google collab if hasattr operator overloading
  • 8. Method Overriding Example class Shape: def area(self): return 0 class Rectangle(Shape): def __init__(self, length, width): self.length = length self.width = width def area(self): return self.length * self.width shape = Shape() rectangle = Rectangle(5, 10) print(shape.area()) # 0 print(rectangle.area()) # 50
  • 9. Operator Overloading Operator Overloading (also called operator ad-hoc polymorphism) allows you to define or change the behavior of built-in operators (like +, -, *, etc.) for custom classes. •Operator Overloading allows you to use operators (such as +, -, *, etc.) to work with objects of your class, just like they work with primitive data types like integers and strings.
  • 10. Operator Overloading What is Operator Overloading? In Python, operator overloading allows you to redefine the meaning of standard operators (+, -, *, etc.) when they are used with objects of your own class. This means you can customize how an operator behaves when applied to your objects.
  • 12. Operator Overloading Example class Book: def __init__(self, pages): self.pages = pages def __add__(self, other): return self.pages + other.pages book1 = Book(150) book2 = Book(200) print(book1 + book2) # 350
  • 13. We use operator overloading when we want to make custom classes behave more like built-in types, especially when using operators such as +, -, *, ==, etc. When Do We Use Operator Overloading?
  • 14. When Do We Use Operator Overloading?
  • 15. When Do We Use Operator Overloading?
  • 16. When Do We Use Operator Overloading?
  • 17. Real-Life Example - Payment System class Payment: def pay(self): pass class CreditCard(Payment): def pay(self): return 'Paid using Credit Card' class Paypal(Payment): def pay(self): return 'Paid using Paypal' def make_payment(payment_method): print(payment_method.pay()) make_payment(CreditCard()) make_payment(Paypal())
  • 18. Benefits of Polymorphism • Code Reusability: Same interface for multiple object types • Extensibility: Easy to add new classes • Maintenance: Easier to maintain and scale code • Cleaner Code: Reduces complex if-else structures