Python Polymorphism
Table of Contents
Description
Polymorphism means "many forms" — it's an object-oriented programming concept that allows functions, methods, or operators to behave differently based on the object they are acting upon.
In Python, polymorphism is achieved through:
Duck Typing
Function Overriding (in inheritance)
Built-in Polymorphic Functions (like len(), +, etc.)
Operator Overloading
Abstract Classes/Interfaces (using abc module)
Prerequisites
- Understanding of classes and objects
- Inheritance basics
- Method definitions
- Experience with built-in functions like len(), +, etc.
Examples
Here's a simple program in Python:
✅ 1. Duck Typing (Dynamic Polymorphism) “If it walks like a duck and quacks like a duck, it's a duck.” class Dog: def speak(self): return "Bark" class Cat: def speak(self): return "Meow" def animal_sound(animal): # Works for any object with a 'speak' method print(animal.speak()) d = Dog() c = Cat() animal_sound(d) # Output: Bark animal_sound(c) # Output: Meow ✅ 2. Function Overriding (Method Polymorphism in Inheritance) class Vehicle: def move(self): print("Vehicle is moving") class Car(Vehicle): def move(self): print("Car is moving") class Bike(Vehicle): def move(self): print("Bike is moving") v = Vehicle() c = Car() b = Bike() v.move() # Output: Vehicle is moving c.move() # Output: Car is moving b.move() # Output: Bike is moving ✅ 3. Built-in Polymorphic Functions print(len("Python")) # Output: 6 (String length) print(len([1, 2, 3])) # Output: 3 (List length) print(len({1: "a", 2: "b"})) # Output: 2 (Dict length) ✅ 4. Operator Overloading class Point: def __init__(self, x, y): self.x = x self.y = y # Overload '+' operator def __add__(self, other): return Point(self.x + other.x, self.y + other.y) def __str__(self): return f"({self.x}, {self.y})" p1 = Point(2, 3) p2 = Point(4, 5) p3 = p1 + p2 print(p3) # Output: (6, 8) ✅ 5. Polymorphism Using Abstract Base Classes from abc import ABC, abstractmethod class Shape(ABC): @abstractmethod def area(self): pass class Square(Shape): def __init__(self, side): self.side = side def area(self): return self.side * self.side class Circle(Shape): def __init__(self, radius): self.radius = radius def area(self): return 3.14 * self.radius * self.radius s = Square(5) c = Circle(3) print(s.area()) # Output: 25 print(c.area()) # Output: 28.26Real-World Applications
Web frameworks (e.g., Flask/Django) handling different HTTP methods with same route
GUI Libraries: Drawing different shapes with the same draw() method
Games: Different enemies having their own attack() method
Machine Learning: fit() method works differently for each model class
Where topic Can Be Applied
Backend systems (handling different object types generically)
APIs and SDKs (providing extensible method hooks)
AI/ML frameworks (supporting polymorphic model APIs)
Game development, GUI frameworks
Resources
WatchTopic video source
A comprehensive video
VisitPython pdf
pdf on topic
Interview Questions
What is polymorphism in Python?
Explain duck typing with an example.
How is function overriding implemented in Python?
What is operator overloading? Give a use case.
How does Python support polymorphism without function overloading?
What are abstract base classes and how are they useful?
How do you implement polymorphism with different class hierarchies?