bigonotes
CS ConceptsOOP

Polymorphism

How one interface can behave differently per object in Python: duck typing, method overriding and operator overloading.

3 min readLast updated

Polymorphism means "many forms": the same interface behaves differently depending on the object behind it. The + operator adds numbers but joins strings. Polymorphism is what lets you add new types to a system without rewriting the code that uses them. Python gives you three ways to get it: duck typing, method overriding and operator overloading.

Duck typing#

The loosest form needs no shared parent class at all. Python doesn't check an object's type before calling a method; it only checks whether the method exists. "If it walks like a duck and quacks like a duck, it's a duck."

class Dog:
    def speak(self):
        print("Woof!")

class Cat:
    def speak(self):
        print("Meow!")

class Duck:
    def speak(self):
        print("Quack!")

# No common parent at all
for animal in [Dog(), Cat(), Duck()]:
    animal.speak()
# Woof!
# Meow!
# Quack!

Python looks up speak on each object (instance, then its class and parents). If it finds it, it calls it; if not, you get AttributeError.

Method overriding#

The second form uses inheritance. Here the classes share a parent, and each subclass redefines a parent method with the same name:

import math

class Shape:
    def area(self):
        raise NotImplementedError

class Circle(Shape):
    def __init__(self, r):
        self.r = r

    def area(self):
        return math.pi * self.r ** 2

class Square(Shape):
    def __init__(self, side):
        self.side = side

    def area(self):
        return self.side ** 2

for shape in [Circle(5), Square(4)]:
    print(round(shape.area(), 2))
# 78.54
# 16

Overriding replaces the parent's behavior entirely. Extending calls super().method() first and then adds to it.

Operator overloading#

The + example from the opening is polymorphism too. Operators are shorthand for dunder methods ("double underscore" methods such as __add__), so each type decides what + means for it:

2 + 3        # (2).__add__(3)
"hi" + "!"   # "hi".__add__("!")
[1] + [2]    # [1].__add__([2])

Because operators are only method calls, you can give your own classes operators:

class Vector:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __add__(self, other):
        # Return a Vector (not a tuple) so v1 + v2 + v3 keeps working
        return Vector(self.x + other.x, self.y + other.y)

v = Vector(1, 2) + Vector(3, 4) + Vector(1, 1)
print(v.x, v.y)  # 5 7

Here are the operators you're most likely to overload, with the dunder each one calls:

Operator Dunder method Example
+ __add__ v1 + v2
- __sub__ v1 - v2
* __mul__ v1 * 2
== __eq__ v1 == v2
< __lt__ v1 < v2
> __gt__ v1 > v2
str(obj) __str__ print(v1)
len(obj) __len__ len(v1)

See Dunder Methods & @property for more.

Duck typing vs overriding#

Duck typing and overriding both let one call do different things, so it helps to see where they differ:

Duck typing Method overriding
Needs a common parent? No Yes
Same method name? Yes Yes
Different behavior? Yes Yes
How the method is found Normal lookup on the object Child class first, then parent

Why it matters in design#

Here's the payoff. Polymorphism replaces type checks with a single call:

# Without polymorphism: every new type means editing this block
for animal in animals:
    if type(animal) == Dog:
        animal.bark()
    elif type(animal) == Cat:
        animal.meow()

# With polymorphism: new types just implement speak()
for animal in animals:
    animal.speak()

You'll see this in nearly every low-level design (LLD) problem, where you model a system as classes:

  • Parking lot: different vehicle types behind one Vehicle interface.
  • Ride booking: different cab types, one calculate_fare().
  • Payments: different payment methods, one pay().

Key takeaways#

  • Polymorphism = same interface, different behavior.
  • Duck typing: Python checks that the method exists, not the type.
  • Overriding: a child redefines a parent's method; super() lets you extend it instead.
  • Every operator maps to a dunder method you can implement.
  • Return your own type from operator methods so expressions chain.
  • Polymorphism lets you add types without changing existing callers (the Open/Closed Principle).
Revision notes · 1 min

Core idea#

Polymorphism means "many forms": the same interface behaves differently depending on the object behind it. It lets you add new types without rewriting the code that uses them.

Key points#

  • Duck typing: Python checks that the method exists, not the object's type. A missing method raises AttributeError.
  • Method overriding: a subclass redefines a parent method with the same name.
  • Overriding replaces the parent's behavior. Extending calls super().method() first and adds to it.
  • Operator overloading: every operator maps to a dunder method you can implement.
  • Return your own type from operator methods, so v1 + v2 + v3 keeps working.
  • Polymorphism replaces if type(x) == Dog chains with one call, like animal.speak().
  • LLD examples: vehicle types in a parking lot, one calculate_fare() per cab type, one pay() per payment method.

Duck typing vs overriding#

Duck typing Method overriding
Common parent needed No Yes
How the method is found Normal lookup on the object Child class first, then parent

Operators and dunders#

Operator Dunder method
+, -, * __add__, __sub__, __mul__
==, <, > __eq__, __lt__, __gt__
str(obj), len(obj) __str__, __len__

Pitfalls#

  • Type checks with if/elif mean every new type edits existing code. That breaks the Open/Closed Principle.
  • An operator method that returns a tuple instead of your type breaks chaining.

Say it in an interview#

  • "Same interface, different behavior."
  • "Python uses duck typing: if the method exists, the call works."

Found a mistake? Email ask@bigonotes.com