Learn Python
>>> oop_fundamentals.polymorphism

Polymorphism in Python

#concept

Polymorphism means objects of different classes can be used through the same interface, each responding to the same method call in its own way. In Python this happens naturally: any object with a matching method name can be used wherever that method is expected, without needing a shared base class or any special declaration — a style often called "duck typing."

#how it works
1

Different classes share a method name

Like speak() or area(), defined independently in each class.

2

Calling code stays agnostic

Code that calls .speak() doesn’t need to know or care exactly which class the object belongs to.

3

Checked only when actually called

Python looks for the method at call time, not ahead of time — if the object has it, the call works.

4

One loop can handle many types

As long as each object supports the expected method, a single loop works across all of them.

5

This is "duck typing"

If it walks like a duck and quacks like a duck, Python treats it as a duck.

#example
class Circle:
def area(self):
return 3.14 * 5 ** 2
 
class Square:
def area(self):
return 4 ** 2
 
shapes = [Circle(), Square()]
 
for shape in shapes:
print(shape.area())
Output
78.5
16
1Circle and Square are unrelated classes, sharing no common parent, but both define an area() method.
2The loop calls shape.area() on each item without checking which class it is — Python simply looks up area() on whatever object it happens to be, at the moment it’s called.
3Each object’s own version of area() runs, producing a different calculation depending on the actual shape.
#use cases
Writing one function or loop that works across many different object types, as long as each supports the expected method
Building plugin-style systems, where any object that implements the right interface can be used interchangeably
Avoiding long chains of type-checking (isinstance) code by relying on consistent method names instead
Making code open to new types later, since anything implementing the right methods will just work without changes to the calling code

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Python yourself — building beats watching.