Polymorphism in Python: A Beginner’s Guide

Introduction

    Polymorphism in Python: A Beginner’s Guide or Polymorphism makes code flexible and reusable.
In simple words, polymorphism in Python means one interface, many forms.
Beginners find it powerful for writing clean object-oriented code.
    This guide explains concepts, examples, and interview questions step-by-step.
if you want to read our previous tutorial then click here…

What is polymorphism in Python?

Polymorphism in Python is an OOP concept.
    It lets objects of different types use the same method name.
Therefore, code can work with different objects the same way.
Put simply: one action, many object-specific behaviors.

Key terms (quick)

  • Duck typing — If it quacks like a duck, it’s a duck.
  • Method overriding — Child class changes parent method.
  • Operator overloading — Operators behave differently per class.

Types of polymorphism (with examples)

1. Duck typing (dynamic polymorphism)

Python focuses on what an object can do.
You do not need explicit interfaces.

class Bird:
def fly(self):
print(“Bird is flying”)

class Airplane:
def fly(self):
print(“Airplane is flying”)

def let_it_fly(thing):
thing.fly()

let_it_fly(Bird()) # Bird is flying
let_it_fly(Airplane()) # Airplane is flying

2. Method overriding (runtime polymorphism)

A subclass provides its own implementation.

method-overriding-python-vbkinfoxyz



class Animal:
    def sound(self):
    print(“Some sound”)

class Dog(Animal):
    def sound(self):
    print(“Bark”)

a = Animal()
d = Dog()
a.sound() # Some sound
d.sound() # Bark

3. Operator overloading

Define how operators work on your objects.

class Point:
    def init(self, x, y):
        self.x, self.y = x, y
    def add(self, other):
        return Point(self.x + other.x, self.y + other.y)
    def repr(self):
        return f”Point({self.x},{self.y})”

p1 = Point(1,2)
p2 = Point(3,4)
print(p1 + p2) # Point(4,6)

class Point:
    def init(self, x, y):
        self.x, self.y = x, y
    def add(self, other):
        return Point(self.x + other.x, self.y + other.y)
    def repr(self):
        return f”Point({self.x},{self.y})”

p1 = Point(1,2)
p2 = Point(3,4)
print(p1 + p2) # Point(4,6)

so this is happen in Polymorphism in Python

Real-life example (simple)

    Think of a remote control.
It can control TV, AC, and music system.
Each device responds to the same “turn_on” command differently.
In code, different classes implement turn_on() the same way.
Thus, polymorphism in Python models this real-life behavior.

class TV:
    def turn_on(self):
        print(“TV on”)

class AC:
    def turn_on(self):
        print(“AC on”)

for device in (TV(), AC()):
    device.turn_on()

Step-by-step example with output

Example: Shape area calculation using polymorphism.

class Shape:
    def area(self):
        raise NotImplementedError

class Circle(Shape):
    def init(self, r):
        self.r = r
    def area(self):
        return 3.14 * self.r * self.r

class Square(Shape):
    def init(self, s):
        self.s = s
    def area(self):
        return self.s * self.s

shapes = [Circle(2), Square(3)]
for s in shapes:
    print(s.area())

Output:
12.56
9

Overloading vs Overriding — Quick comparison

FeatureOverloadingOverriding
WhatSame name, different signatures (not native in Python)Same name, same signature in subclass
WhenCompile-time in some languagesRuntime in Python
Python supportLimited (use default args / *args)Full (method overriding)

Common interview questions (with answers)

  1. What is polymorphism in Python?
    • Answer: One interface, many implementations. Objects behave differently to the same method call.
  2. Explain duck typing.
    • Answer: Python checks for method existence, not object type.
  3. How to implement operator overloading?
    • Answer: Define magic methods like __add__, __eq__, etc.
  4. Difference between overloading and overriding?
    • Answer: Overloading = same name multiple signatures. Overriding = child replaces parent method.
  5. Can functions be polymorphic?
    • Answer: Yes. Functions accept different object types as long as they provide required methods.
  6. What are Protocols and how do they help?
    • Answer: typing.Protocol defines expected method signatures for static typing.
  7. How to use functools.singledispatch?
    • Answer: It allows simple single-dispatch function polymorphism based on arg type.

Advanced: patterns & tips

  • Use ABCs (abc module) when you need formal interfaces.
  • Use typing.Protocol for lightweight interfaces and better type checking.
  • Use functools.singledispatch for function-based polymorphism.
  • Prefer duck typing for simple cases.
  • For complex APIs, document expected methods clearly.

Example: singledispatch

from functools import singledispatch

@singledispatch
def show(x):
    print("Default:", x)

@show.register
def _(x: int):
    print("Integer:", x)

show("hello")  # Default: hello
show(5)        # Integer: 5

FAQ

Q1: Is polymorphism same as inheritance?
A: Not the same. Inheritance enables polymorphism, but polymorphism is about interface use.

Q2: Does Python support method overloading?
A: Not directly. Use default args or *args to simulate it.

Q3: Why use polymorphism?
A: For cleaner, scalable, and extensible code.

Q4: Is duck typing safe?
A: Mostly. Use tests and type hints to catch mistakes early.

Q5: Where to read official docs?
A: See Python docs (https://docs.python.org/3/) and tutorials at W3Schools (https://www.w3schools.com/python/).


Pro Tip

If you want safer static checks, add typing.Protocol and mypy checks. This keeps duck typing flexible, yet verifiable.


Conclusion

    so Polymorphism in Python: A Beginner’s Guide in Polymorphism makes your Python code flexible and easier to extend.
You can start small with duck typing. Then add ABCs or Protocols as needed.
Try the examples above and tweak them.
Want a detailed example for your project? Tell me your scenario and I’ll write code for you.

Question for readers: What example would you like next — a web app, a data class, or a game object? Share in the comments!


Did you know? Python’s dynamic nature makes polymorphism very natural.
For tutorials and more examples, visit vbkinfo.xyz.


Comments

2 responses to “Polymorphism in Python: A Beginner’s Guide”

  1. Shlok Avatar
    Shlok

    Nice job

  2. Shlok Avatar
    Shlok

    What is meaning this dog or cat and 🐦 1255

Leave a Reply

Your email address will not be published. Required fields are marked *