SlideShare a Scribd company logo
POLYMORPHISM IN PYTHON
(An Overview)
 Polymorphism is a fundamental concept in object-
oriented programming (OOP) that allows objects of
different types to be treated as objects of a common
type. Python supports polymorphism through both
duck typing and method overloading.
DUCK TYPING
 In Python, polymorphism is often achieved through
duck typing. This means that the type or the class
of an object is less important than the methods and
properties it possesses. If an object supports a
particular method or property, it can be used
interchangeably with other objects that support the
same method or property.
DUCK TYPING - EXAMPLE
class Dog:
def speak(self):
return "Woof!"
class Cat:
def speak(self):
return "Meow!"
def animal_speak(animal):
return animal.speak()
dog = Dog()
cat = Cat()
print(animal_speak(dog)) # Output: Woof!
print(animal_speak(cat)) # Output: Meow!
METHOD OVERLOADING
 Python does not support traditional method
overloading (having multiple methods with the
same name but different parameters) like some
other languages do. However, you can achieve a
form of method overloading using default parameter
values and variable numbers of arguments.
METHOD OVERLOADING - EXAMPLE
class MathOperations:
def add(self, x, y=None, z=None):
if y is not None and z is not None:
return x + y + z
elif y is not None:
return x + y
else:
return x
math_ops = MathOperations()
print(math_ops.add(2)) # Output: 2
print(math_ops.add(2, 3)) # Output: 5
print(math_ops.add(2, 3, 4)) # Output: 9
ADVANTAGES
 Polymorphism allows for the creation of more generic and
reusable code. A single interface can be used to represent
different types of objects, promoting code reusability.
 Polymorphism makes it easier to extend and modify code.
New classes can be added without altering existing code, as
long as they adhere to the common interface.
 Polymorphic code tends to be more readable and
maintainable. Code that operates on a higher level of
abstraction (e.g., using interfaces or abstract classes) is often
easier to understand and modify.
 Polymorphism allows for dynamic binding, where the specific
method implementation is determined at runtime. This
enables more flexibility in program execution and facilitates
better adaptation to changes.
DISADVANTAGES
 Overuse or misuse of polymorphism can lead to complex and
hard-to-understand code. It's important to strike a balance
and use polymorphism judiciously where it adds value to the
design.
 Dynamic dispatch, a mechanism that enables polymorphism,
may introduce a slight performance overhead compared to
static binding. However, in many cases, the performance
impact is negligible.
 Since method resolution is determined at runtime, errors
related to missing or incorrect methods may not be caught
until the program is executed. This can make debugging more
challenging.
 Understanding and implementing polymorphism might have a
steeper learning curve for programmers new to object-
oriented concepts. It requires a solid understanding of
inheritance, interfaces, and dynamic dispatch.
THANK YOU

More Related Content

Similar to Polymorphism in Python

Oop by edgar lagman jr
Oop by edgar lagman jr Oop by edgar lagman jr
Oop by edgar lagman jr
Jun-jun Lagman
 
C++ OOP Implementation
C++ OOP ImplementationC++ OOP Implementation
C++ OOP Implementation
Fridz Felisco
 

Similar to Polymorphism in Python (20)

object oriented programming(oops)
object oriented programming(oops)object oriented programming(oops)
object oriented programming(oops)
 
Object oriented programing
Object oriented programingObject oriented programing
Object oriented programing
 
Oops in PHP By Nyros Developer
Oops in PHP By Nyros DeveloperOops in PHP By Nyros Developer
Oops in PHP By Nyros Developer
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
LectureNotes-01-DSA
LectureNotes-01-DSALectureNotes-01-DSA
LectureNotes-01-DSA
 
Mcs024
Mcs024Mcs024
Mcs024
 
Java OOPs Concepts.docx
Java OOPs Concepts.docxJava OOPs Concepts.docx
Java OOPs Concepts.docx
 
polymorphism
polymorphismpolymorphism
polymorphism
 
Oop by edgar lagman jr
Oop by edgar lagman jr Oop by edgar lagman jr
Oop by edgar lagman jr
 
C++ programing lanuage
C++ programing lanuageC++ programing lanuage
C++ programing lanuage
 
Polymorphism.pptx
Polymorphism.pptxPolymorphism.pptx
Polymorphism.pptx
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism in oop
Polymorphism in oopPolymorphism in oop
Polymorphism in oop
 
Promoting Polymorphism
Promoting PolymorphismPromoting Polymorphism
Promoting Polymorphism
 
Top 30 Technical interview questions
Top 30 Technical interview questionsTop 30 Technical interview questions
Top 30 Technical interview questions
 
Need of object oriented programming
Need of object oriented programmingNeed of object oriented programming
Need of object oriented programming
 
C# concepts
C# conceptsC# concepts
C# concepts
 
POP vs OOP Introduction
POP vs OOP IntroductionPOP vs OOP Introduction
POP vs OOP Introduction
 
C++ OOP Implementation
C++ OOP ImplementationC++ OOP Implementation
C++ OOP Implementation
 

More from To Sum It Up

More from To Sum It Up (20)

Django Framework Interview Guide - Part 1
Django Framework Interview Guide - Part 1Django Framework Interview Guide - Part 1
Django Framework Interview Guide - Part 1
 
Artificial intelligence ( AI ) | Guide
Artificial intelligence ( AI )  |  GuideArtificial intelligence ( AI )  |  Guide
Artificial intelligence ( AI ) | Guide
 
On Page SEO (Search Engine Optimization)
On Page SEO (Search Engine Optimization)On Page SEO (Search Engine Optimization)
On Page SEO (Search Engine Optimization)
 
Prompt Engineering | Beginner's Guide - For You
Prompt Engineering | Beginner's Guide - For YouPrompt Engineering | Beginner's Guide - For You
Prompt Engineering | Beginner's Guide - For You
 
Natural Language Processing (NLP) | Basics
Natural Language Processing (NLP) | BasicsNatural Language Processing (NLP) | Basics
Natural Language Processing (NLP) | Basics
 
It's Machine Learning Basics -- For You!
It's Machine Learning Basics -- For You!It's Machine Learning Basics -- For You!
It's Machine Learning Basics -- For You!
 
DSA Question Bank
DSA Question BankDSA Question Bank
DSA Question Bank
 
Web API - Overview
Web API - OverviewWeb API - Overview
Web API - Overview
 
CSS Overview
CSS OverviewCSS Overview
CSS Overview
 
HTML Overview
HTML OverviewHTML Overview
HTML Overview
 
EM Algorithm
EM AlgorithmEM Algorithm
EM Algorithm
 
User story mapping
User story mappingUser story mapping
User story mapping
 
User stories
User storiesUser stories
User stories
 
Problem solving using computers - Unit 1 - Study material
Problem solving using computers - Unit 1 - Study materialProblem solving using computers - Unit 1 - Study material
Problem solving using computers - Unit 1 - Study material
 
Problem solving using computers - Chapter 1
Problem solving using computers - Chapter 1 Problem solving using computers - Chapter 1
Problem solving using computers - Chapter 1
 
Quality Circle | Case Study on Self Esteem | Team Opus Geeks.pdf
Quality Circle | Case Study on Self Esteem | Team Opus Geeks.pdfQuality Circle | Case Study on Self Esteem | Team Opus Geeks.pdf
Quality Circle | Case Study on Self Esteem | Team Opus Geeks.pdf
 
Multimedia Content and Content Acquisition
Multimedia Content and Content AcquisitionMultimedia Content and Content Acquisition
Multimedia Content and Content Acquisition
 
PHP Arrays_Introduction
PHP Arrays_IntroductionPHP Arrays_Introduction
PHP Arrays_Introduction
 
System Calls - Introduction
System Calls - IntroductionSystem Calls - Introduction
System Calls - Introduction
 
Leadership
LeadershipLeadership
Leadership
 

Recently uploaded

Hall booking system project report .pdf
Hall booking system project report  .pdfHall booking system project report  .pdf
Hall booking system project report .pdf
Kamal Acharya
 
Fruit shop management system project report.pdf
Fruit shop management system project report.pdfFruit shop management system project report.pdf
Fruit shop management system project report.pdf
Kamal Acharya
 

Recently uploaded (20)

retail automation billing system ppt.pptx
retail automation billing system ppt.pptxretail automation billing system ppt.pptx
retail automation billing system ppt.pptx
 
Supermarket billing system project report..pdf
Supermarket billing system project report..pdfSupermarket billing system project report..pdf
Supermarket billing system project report..pdf
 
Hall booking system project report .pdf
Hall booking system project report  .pdfHall booking system project report  .pdf
Hall booking system project report .pdf
 
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-5 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical Engineering
 
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data StreamKIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...
 
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
 
Fruit shop management system project report.pdf
Fruit shop management system project report.pdfFruit shop management system project report.pdf
Fruit shop management system project report.pdf
 
An improvement in the safety of big data using blockchain technology
An improvement in the safety of big data using blockchain technologyAn improvement in the safety of big data using blockchain technology
An improvement in the safety of big data using blockchain technology
 
2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge
 
Maestro Scripting Language CNC programacion
Maestro Scripting Language CNC programacionMaestro Scripting Language CNC programacion
Maestro Scripting Language CNC programacion
 
KIT-601 Lecture Notes-UNIT-4.pdf Frequent Itemsets and Clustering
KIT-601 Lecture Notes-UNIT-4.pdf Frequent Itemsets and ClusteringKIT-601 Lecture Notes-UNIT-4.pdf Frequent Itemsets and Clustering
KIT-601 Lecture Notes-UNIT-4.pdf Frequent Itemsets and Clustering
 
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and VisualizationKIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
 
NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...
NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...
NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...
 
Dairy management system project report..pdf
Dairy management system project report..pdfDairy management system project report..pdf
Dairy management system project report..pdf
 
Online book store management system project.pdf
Online book store management system project.pdfOnline book store management system project.pdf
Online book store management system project.pdf
 
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES  INTRODUCTION UNIT-IENERGY STORAGE DEVICES  INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
 
Electrical shop management system project report.pdf
Electrical shop management system project report.pdfElectrical shop management system project report.pdf
Electrical shop management system project report.pdf
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 

Polymorphism in Python

  • 2.  Polymorphism is a fundamental concept in object- oriented programming (OOP) that allows objects of different types to be treated as objects of a common type. Python supports polymorphism through both duck typing and method overloading.
  • 3. DUCK TYPING  In Python, polymorphism is often achieved through duck typing. This means that the type or the class of an object is less important than the methods and properties it possesses. If an object supports a particular method or property, it can be used interchangeably with other objects that support the same method or property.
  • 4. DUCK TYPING - EXAMPLE class Dog: def speak(self): return "Woof!" class Cat: def speak(self): return "Meow!" def animal_speak(animal): return animal.speak() dog = Dog() cat = Cat() print(animal_speak(dog)) # Output: Woof! print(animal_speak(cat)) # Output: Meow!
  • 5. METHOD OVERLOADING  Python does not support traditional method overloading (having multiple methods with the same name but different parameters) like some other languages do. However, you can achieve a form of method overloading using default parameter values and variable numbers of arguments.
  • 6. METHOD OVERLOADING - EXAMPLE class MathOperations: def add(self, x, y=None, z=None): if y is not None and z is not None: return x + y + z elif y is not None: return x + y else: return x math_ops = MathOperations() print(math_ops.add(2)) # Output: 2 print(math_ops.add(2, 3)) # Output: 5 print(math_ops.add(2, 3, 4)) # Output: 9
  • 7. ADVANTAGES  Polymorphism allows for the creation of more generic and reusable code. A single interface can be used to represent different types of objects, promoting code reusability.  Polymorphism makes it easier to extend and modify code. New classes can be added without altering existing code, as long as they adhere to the common interface.  Polymorphic code tends to be more readable and maintainable. Code that operates on a higher level of abstraction (e.g., using interfaces or abstract classes) is often easier to understand and modify.  Polymorphism allows for dynamic binding, where the specific method implementation is determined at runtime. This enables more flexibility in program execution and facilitates better adaptation to changes.
  • 8. DISADVANTAGES  Overuse or misuse of polymorphism can lead to complex and hard-to-understand code. It's important to strike a balance and use polymorphism judiciously where it adds value to the design.  Dynamic dispatch, a mechanism that enables polymorphism, may introduce a slight performance overhead compared to static binding. However, in many cases, the performance impact is negligible.  Since method resolution is determined at runtime, errors related to missing or incorrect methods may not be caught until the program is executed. This can make debugging more challenging.  Understanding and implementing polymorphism might have a steeper learning curve for programmers new to object- oriented concepts. It requires a solid understanding of inheritance, interfaces, and dynamic dispatch.