KEMBAR78
Polymorphism_in_Python_Programming_Language | PPTX
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

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 PolymorphismImportant? • • Flexibility and code reusability • • Generalization: One function works for multiple object types • • Enhances readability and maintenance
  • 4.
    Types of Polymorphismin Python • 1. Duck Typing • 2. Method Overriding (Runtime Polymorphism) • 3. Operator Overloading
  • 5.
    Duck Typing Example •DuckTyping 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 classCat: 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 Writea program to illustrate duck typing in Python using Banking as a scenario //use google collab if hasattr operator overloading
  • 8.
    Method Overriding Example classShape: 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 isOperator 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.
  • 11.
  • 12.
    Operator Overloading Example classBook: 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 operatoroverloading 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 WeUse Operator Overloading?
  • 15.
    When Do WeUse Operator Overloading?
  • 16.
    When Do WeUse 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