Last modified: Dec 04, 2026

Python Class Polymorphism Guide

Polymorphism sounds fancy. It isn't.

It's just a way of saying "different objects can respond to the same call in their own way." That's it. Once you see it in code, the word stops being scary and starts being useful.

In Python, polymorphism shows up everywhere. You probably already use it without knowing the name. Let's break it down with real examples you can run today.

What Polymorphism Actually Means

The word comes from Greek: "poly" means many, "morph" means forms. So polymorphism is about one thing taking many forms.

In object-oriented code, that usually means one method name, many behaviors. You call speak() on a dog and it barks. You call speak() on a cat and it meows. Same call, different result.

Here's a quick example:


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

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

for animal in [Dog(), Cat()]:
    print(animal.speak())

Woof!
Meow!

Notice there's no shared parent class here. Both objects just happen to have a speak() method. Python doesn't care about types. It cares about behavior.

That's the core idea. Everything else is a variation on it.

Polymorphism Through Inheritance

The most common form you'll see in tutorials is polymorphism via inheritance. A parent class defines a method, and child classes override it.

If you're new to class basics, start with the beginner guide to Python classes before going further. It'll make the next part click faster.

Here's a classic shape example:


class Shape:
    def area(self):
        raise NotImplementedError("Subclasses must implement area()")

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

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

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

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

shapes = [Circle(5), Square(4)]
for shape in shapes:
    print(f"{shape.__class__.__name__} area: {shape.area()}")

Circle area: 78.5
Square area: 16

Each subclass implements area() its own way. The loop doesn't know or care which class it's dealing with. It just calls the method.

This is the pattern you'll use most often. And it's exactly why inheritance exists in the first place. If you want more depth on the parent-child relationship, there's a solid walkthrough on how inheritance works in Python that pairs well with this article.

Method Overriding in Practice

Overriding is the engine behind inheritance-based polymorphism. A child class replaces a parent method with its own version.

Here's what that looks like when you also want to reuse the parent logic:


class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary

    def role(self):
        return "Employee"

class Manager(Employee):
    def role(self):
        return "Manager"

class Developer(Employee):
    def role(self):
        return "Developer"

staff = [Employee("Ana", 50000), Manager("Ben", 80000), Developer("Cara", 70000)]
for person in staff:
    print(f"{person.name} is a {person.role()}")

Ana is a Employee
Ben is a Manager
Cara is a Developer

Simple, readable, and it scales. Add a new subclass tomorrow and the loop still works. That's the real value of polymorphism: you write the calling code once.

Duck Typing: Python's Own Flavor

Here's where Python differs from Java or C#. You don't need a shared base class to get polymorphic behavior.

If it walks like a duck and quacks like a duck, Python treats it like a duck. That's duck typing, and it's baked into the language.


class PDFReport:
    def generate(self):
        return "Generating PDF report"

class CSVReport:
    def generate(self):
        return "Generating CSV report"

class HTMLReport:
    def generate(self):
        return "Generating HTML report"

def export(report):
    print(report.generate())

for r in [PDFReport(), CSVReport(), HTMLReport()]:
    export(r)

Generating PDF report
Generating CSV report
Generating HTML report

No inheritance. No abstract classes. Just a shared method name. Python's export() function doesn't check types at all.

Some developers love this. Some hate it. I lean toward loving it, but only when the method names are obvious. If your classes are named well, duck typing makes code feel natural.

Polymorphism with Built-in Functions

Python's built-ins are polymorphic too. Take len(). It works on strings, lists, tuples, dicts, and any custom class that implements __len__.


print(len("hello"))
print(len([1, 2, 3]))
print(len({"a": 1, "b": 2}))

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

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

print(len(Playlist(["Song A", "Song B", "Song C"])))

5
3
2
3

That __len__ method is a dunder method, and dunder methods are the backbone of Python polymorphism. If you want the full list, the dunder methods reference is worth bookmarking. You'll come back to it often.

Same idea applies to +, ==, print(), and basically every operator in the language. They all map to dunder methods under the hood.

Polymorphism with Class Methods and Static Methods

You can also get polymorphism from class methods. This is less common but handy when you want subclasses to configure themselves differently.


class Database:
    @classmethod
    def connect(cls):
        return f"Connecting to {cls.__name__}"

class Postgres(Database):
    pass

class MySQL(Database):
    pass

for db in [Postgres, MySQL]:
    print(db.connect())

Connecting to Postgres
Connecting to MySQL

Because cls refers to the actual class, each subclass gets its own name. Same method, different output. That's polymorphism again.

If class methods are new to you, check out these practical class method examples. They cover the pattern in more detail.

When to Use Polymorphism (and When Not To)

Use it when you have multiple objects that share behavior but differ in details. Reports, shapes, payment methods, notification channels. That's the sweet spot.

Don't force it. If you only have one class, you don't need polymorphism. If your subclasses all do the exact same thing, you don't need overriding either. Keep it simple until the problem actually demands it.

I've seen codebases where every class inherits from three others and nothing is clear. That's not polymorphism. That's a puzzle.

Common Mistakes to Avoid

Here are the ones I see most often:

Forgetting to override. If a subclass doesn't override the parent method, it silently uses the parent version. That can hide bugs.

Using isinstance() everywhere. If you're constantly checking types before calling a method, you're fighting polymorphism instead of using it.

Deep inheritance chains. Three levels is usually fine. Five is a smell. Prefer composition when things get deep.

Ignoring the interface. Duck typing is flexible, but it means you should document what methods callers expect. A short docstring saves hours later.

Putting It All Together

Here's a small but realistic example that combines several ideas:


class Notification:
    def send(self, message):
        raise NotImplementedError

class Email(Notification):
    def send(self, message):
        print(f"Email: {message}")

class SMS(Notification):
    def send(self, message):
        print(f"SMS: {message}")

class Push(Notification):
    def send(self, message):
        print(f"Push: {message}")

def notify_all(channels, message):
    for channel in channels:
        channel.send(message)

notify_all([Email(), SMS(), Push()], "Server is down")

Email: Server is down
SMS: Server is down
Push: Server is down

One function, three behaviors, zero type checks. Add a Slack channel tomorrow and notify_all() doesn't change at all.

That's the payoff. You write less code as the system grows, not more.

Final Thoughts

Polymorphism in Python is less about theory and more about trusting your objects to know what to do.

You define a method name. You let each class decide how to handle it. Then you write calling code that doesn't need to change when new types show up.

Start with the inheritance version, since it's the most explicit. Move to duck typing once you're comfortable. And keep your class hierarchies shallow.

If you're still shaky on the foundations, spend an afternoon on building classes with arguments. It's the fastest way to make everything else feel natural.

Then come back here and reread the examples. They'll look obvious. That's how you know it clicked.