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

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 (12)

Endpoint Security - - IP layer Attacks and Vulnerabilities
Endpoint Security - - IP layer Attacks and VulnerabilitiesEndpoint Security - - IP layer Attacks and Vulnerabilities
Endpoint Security - - IP layer Attacks and Vulnerabilities
abigailjudith8
 
Endpoint Security - Network Security Infrastructure
Endpoint Security - Network Security InfrastructureEndpoint Security - Network Security Infrastructure
Endpoint Security - Network Security Infrastructure
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
 
Endpoint Security - - IP layer Attacks and Vulnerabilities
Endpoint Security - - IP layer Attacks and VulnerabilitiesEndpoint Security - - IP layer Attacks and Vulnerabilities
Endpoint Security - - IP layer Attacks and Vulnerabilities
abigailjudith8
 
Endpoint Security - Network Security Infrastructure
Endpoint Security - Network Security InfrastructureEndpoint Security - Network Security Infrastructure
Endpoint Security - Network Security Infrastructure
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)

Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
angelo60207
 
The State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry ReportThe State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry Report
Liveplex
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOMEstablish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Anchore
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementaryMurdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FMEEnabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Infrassist Technologies Pvt. Ltd.
 
Cisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdfCisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdf
superdpz
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Safe Software
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely
 
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
angelo60207
 
The State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry ReportThe State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry Report
Liveplex
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOMEstablish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Anchore
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementaryMurdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FMEEnabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Infrassist Technologies Pvt. Ltd.
 
Cisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdfCisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdf
superdpz
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Safe Software
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely
 
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