Last modified: Dec 04, 2026

Python Polymorphism Examples

Polymorphism sounds academic. It really isn't.

The word comes from Greek and means "many forms", but in Python it boils down to one idea: the same call can behave differently depending on the object you're calling it on. You already use polymorphism every time you write len() on a string, a list, or a dictionary. Same function name, different work under the hood. That's the whole concept, and the rest of this article is just variations on it.

Let's look at real code.

Duck Typing: The Python Way

Python doesn't care about types at call time. It cares about behavior.

If an object has the methods you need, it works. No base class required, no interface to implement. This is often called duck typing, after the old saying: if it walks like a duck and quacks like a duck, it's a duck. Here's a quick example with two unrelated classes:


class Dog:
    def speak(self):
        return "Woof"

class Robot:
    def speak(self):
        return "Beep boop"

# No shared parent. Python doesn't care.
for thing in (Dog(), Robot()):
    print(thing.speak())

Woof
Beep boop

Notice Dog and Robot share nothing. Still, the loop works because both objects respond to speak. That's polymorphism in its purest Python form. If you're new to building objects, start with the basics in this beginner guide to Python classes before going further.

Some developers coming from Java find this uncomfortable. I get it. But duck typing is why Python code stays short.

Method Overriding with Inheritance

Inheritance is the classic route to polymorphism.

A child class redefines a method from its parent, and the same call produces different results depending on which object you use. Consider a small shape hierarchy. Each shape has an area method, but the math changes per class:


class Shape:
    def area(self):
        return 0

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

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

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

    def area(self):
        return 3.14 * self.radius ** 2

# One loop, different behavior per object
for shape in (Square(4), Circle(2)):
    print(f"{type(shape).__name__}: {shape.area()}")

Square: 16
Circle: 12.56

That single loop is the payoff. You write the calling code once and it works for every shape you add later. If you want a deeper walkthrough of the parent-child mechanics, the Python class polymorphism guide covers it step by step. And if you're still shaky on how a child class picks up from a parent, this single inheritance example is a good warm-up.

One caveat. Overriding works best when the parent defines a meaningful default or raises NotImplementedError. A silent return 0 can hide bugs.

Polymorphism Without Inheritance

You don't need a shared base class at all.

Python's built-in functions are already polymorphic. len() works on strings, lists, tuples, and dicts because each type implements the same underlying protocol. You can do the same thing with your own classes by implementing the right dunder methods. Here's a tiny example where two unrelated classes both support len():


class Playlist:
    def __init__(self, songs):
        self.songs = songs

    def __len__(self):
        return len(self.songs)

class Team:
    def __init__(self, members):
        self.members = members

    def __len__(self):
        return len(self.members)

print(len(Playlist(["a", "b", "c"])))  # 3
print(len(Team(["ann", "bob"])))       # 2

3
2

Same function, two classes, zero shared ancestry. That's protocol-based polymorphism, and it's everywhere in Python once you notice it. If you want the full menu of these hooks, keep a Python dunder methods list handy. It's the kind of reference you'll open more than you expect.

Honestly, this is my favorite flavor of polymorphism. It keeps class hierarchies shallow.

Operator Overloading

Operators are methods in disguise.

When you write a + b, Python calls __add__ on a. Override it and + means whatever you want for your type. A vector class is the standard example:


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

    def __add__(self, other):
        return Vector(self.x + other.x, self.y + other.y)

    def __repr__(self):
        return f"Vector({self.x}, {self.y})"

v1 = Vector(1, 2)
v2 = Vector(3, 4)
print(v1 + v2)

Vector(4, 6)

The + operator now behaves differently for Vector than it does for int or str. That's polymorphism again, just at the syntax level. Don't go overboard here. Overloading + to mean something surprising will confuse anyone reading your code six months from now, including you.

Abstract Base Classes: When You Want Rules

Sometimes duck typing is too loose.

You want a guarantee that every subclass implements certain methods. That's where abc comes in. Define an abstract method and Python refuses to instantiate any subclass that skips it. Here's the shape example tightened up:


from abc import ABC, abstractmethod

class Shape(ABC):
    @abstractmethod
    def area(self):
        pass

class Triangle(Shape):
    def __init__(self, base, height):
        self.base = base
        self.height = height

    def area(self):
        return 0.5 * self.base * self.height

print(Triangle(4, 6).area())  # 12.0

12.0

Try instantiating Shape directly and you'll get a TypeError telling you the abstract method wasn't implemented. That early failure is worth a lot in bigger codebases. If you're building out a hierarchy, it helps to know the types of Python inheritance you can combine with this pattern.

Why It Matters in Practice

Polymorphism keeps calling code stable.

Picture a payment system. You write a checkout function that calls pay() on whatever object it receives. Credit cards, PayPal, crypto, whatever comes next. The checkout function never changes; you just add a new class. That's the real value, and it has nothing to do with clever syntax. It's about isolating change. New behavior goes into new classes instead of sprawling if/elif chains.

Small tip: keep method signatures consistent across related classes. If one pay takes an amount and another takes nothing, the abstraction leaks and your loop breaks.

If you want to see how a class accepts configuration through __init__, the guide on classes with arguments pairs well with these examples. And once you're comfortable, review your own projects for repeated conditionals. Those are usually polymorphism waiting to happen.

That's polymorphism in Python. Same call, many forms, and less code to maintain.