Python Polymorphism

Python · Functions & OOP

Theory

Polymorphism means 'many forms': the same method call can behave differently depending on the object that receives it. If Dog and Cat both define make_sound(), a loop can call animal.make_sound() on each one without checking its type - the dog prints Woof and the cat prints Meow.

Python relies on duck typing: if an object has the method you call, it works, whether or not the classes share a parent ('if it quacks like a duck...'). Polymorphism also appears through inheritance, where subclasses override a parent method, and in built-ins: len() works on strings, lists and dicts, and + adds numbers but joins strings, because each type implements the operation in its own way.

Why we use it

It lets you write general code that works with many types at once. New classes can be added later without changing the code that uses them, as long as they provide the same method names.

When to use it

Use it whenever several types share a common action (pay, export, make_sound) and the caller should not care which type it has. If you find long if/elif chains checking type(), polymorphism is usually the cleaner fix.

How it works

  1. for animal in [Dog(), Cat()]
  2. Take the next object
  3. Call animal.make_sound()
  4. Is the object a Dog? Yes: Run Dog.make_sound -> Woof. No: Run Cat.make_sound -> Meow.
  5. More animals? Yes: Go to the next object. No: Finish the loop.
  6. Output: Woof, Meow

Syntax

def speak(animal):
    animal.make_sound()

Example

class Dog:
    def make_sound(self): print('Woof')
class Cat:
    def make_sound(self): print('Meow')

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic