Python
Python Basics
- Introduction to Python and Its History
- Python Syntax and Indentation
- Python Variables and Data Types
- Dynamic and Strong Typing
- Comments and Docstrings
- Taking User Input (input())
- Printing Output (print())
- Python Operators (Arithmetic, Logical, Comparison)
- Type Conversion and Casting
- Escape Characters and Raw Strings
Data Structures in Python
- Lists
- Dictionaries
- Dictionary Comprehensions
- Strings and String Manipulation
- Tuples
- Python Sets: Unordered Collections
- List Comprehensions and Generator Expressions
- Set Comprehensions
- String Formatting
- Indexing and Slicing
Control Flow and Loops
- Conditional Statements: if, elif, and else
- Loops and Iteration
- While Loops
- Nested Loops
- Loop Control Statements
- Iterators and Iterables
- List, Dictionary, and Set Iterations
Functions and Scope
- Defining and Calling Functions (`def`)
- Function Arguments (`*args`, `**kwargs`)
- Default Arguments and Keyword Arguments
- Lambda Functions
- Global and Local Scope
- Function Return Values
- Recursion in Python
Object-Oriented Programming (OOP)
- Object-Oriented Programming
- Classes and Objects
- the `__init__()` Constructor
- Instance Variables and Methods
- Class Variables and `@classmethod`
- Encapsulation and Data Hiding
- Inheritance and Subclasses
- Method Overriding and super()
- Polymorphism
- Magic Methods and Operator Overloading
- Static Methods
- Abstract Classes and Interfaces
Python Programs
- Array : Find median in an integer array
- Array : Find middle element in an integer array
- Array : Find out the duplicate in an array
- Array : Find print all subsets in an integer array
- Program : Array : Finding missing number between from 1 to n
- Array : Gap and Island problem
- Python Program stock max profit
- Reverse words in Python
- Python array duplicate program
- Coin change problem in python
- Python Write fibonacci series program
- Array : find all the pairs whose sum is equal to a given number
- Find smallest and largest number in array
- Iterate collections
- List comprehensions
- Program: Calculate Pi in Python
- String Formatting in Python
Polymorphism in Python: A Beginner’s Friendly Guide with Real-Life Examples
When you start learning programming, especially object-oriented programming (OOP), one term that comes up often is polymorphism. While it may sound complex at first, it’s actually a very practical and helpful concept. In this article, we’ll explore polymorphism in Python in a way that’s easy to understand, even if you’re just getting started with coding.
What is Polymorphism?
The word polymorphism comes from Greek and means “many forms.” In the programming world, it refers to the ability of different classes to be treated as instances of the same class through a shared interface.
In simpler terms, polymorphism allows the same method or operation to behave differently based on the object it is acting upon.
This might sound abstract, so let’s take a real-world example first.
Real-Life Analogy
Imagine you have a remote control. You can use it to operate a TV, a sound system, or even an air conditioner. The button labels remain the same — like “Power” or “Volume” — but the effect depends on the device. Turning on the TV is different from turning on the AC, even though you press the same “Power” button.
This is polymorphism at work: one interface, different behaviors.
Why is Polymorphism Important?
Polymorphism offers several benefits:
- Code reusability: Write less code and use it in more ways.
- Flexibility: Easily extend code without changing the core logic.
- Scalability: Add new features or classes without affecting existing code.
These traits make your programs easier to maintain and expand over time.
Types of Polymorphism in Python
Polymorphism in Python is mainly of two types:
- Compile-Time Polymorphism (Static Polymorphism)
- Run-Time Polymorphism (Dynamic Polymorphism)
Let’s explore each type in detail.
1. Compile-Time Polymorphism (Method Overloading)
Python does not support traditional method overloading like some other languages (such as Java or C++). However, you can mimic it using default arguments or variable-length arguments.
Example Using Default Arguments:
class Calculator: def add(self, a, b=0, c=0): return a + b + c
calc = Calculator()print(calc.add(5)) # Output: 5print(calc.add(5, 10)) # Output: 15print(calc.add(5, 10, 20)) # Output: 35
Here, the add()
method works with different numbers of inputs, showing a kind of “method overloading” behavior.
2. Run-Time Polymorphism (Method Overriding)
This is the more common form of polymorphism in Python and involves overriding methods in child classes.
Example Using Method Overriding:
class Animal: def speak(self): print("The animal makes a sound")
class Dog(Animal): def speak(self): print("The dog barks")
class Cat(Animal): def speak(self): print("The cat meows")
# Polymorphism in actionanimals = [Dog(), Cat()]
for animal in animals: animal.speak()
Output:
The dog barksThe cat meows
Although the speak()
method is called on each object, the output depends on the actual class of the object — Dog or Cat.
Polymorphism with Functions and Objects
Functions in Python can also show polymorphic behavior when they accept objects of different classes but treat them in a unified way.
Example:
class Bird: def fly(self): print("Bird can fly")
class Airplane: def fly(self): print("Airplane can fly")
def let_it_fly(flier): flier.fly()
let_it_fly(Bird()) # Output: Bird can flylet_it_fly(Airplane()) # Output: Airplane can fly
Here, let_it_fly()
doesn’t care what type of object it’s dealing with, as long as the object has a fly()
method. This is classic duck typing, another core Python philosophy: “If it walks like a duck and quacks like a duck, it’s a duck.”
Polymorphism with Inheritance
Polymorphism is tightly connected to inheritance, another pillar of OOP. Through inheritance, child classes inherit methods and attributes from a parent class, and can override or extend them.
Example:
class Vehicle: def start(self): print("Starting the engine")
class Car(Vehicle): def start(self): print("Car engine started")
class Bike(Vehicle): def start(self): print("Bike engine started")
vehicles = [Car(), Bike()]
for v in vehicles: v.start()
Output:
Car engine startedBike engine started
Although we’re calling the same method start()
, each vehicle responds differently.
Abstract Classes and Polymorphism
Python provides the abc
module to create abstract base classes, which helps enforce polymorphism in a structured way.
Example with Abstract Class:
from abc import ABC, abstractmethod
class Shape(ABC): @abstractmethod def area(self): pass
class Circle(Shape): def __init__(self, radius): self.radius = radius
def area(self): return 3.14 * self.radius * self.radius
class Square(Shape): def __init__(self, side): self.side = side
def area(self): return self.side * self.side
shapes = [Circle(5), Square(4)]
for shape in shapes: print("Area:", shape.area())
This is a clean way to enforce that every subclass of Shape
must implement an area()
method.
Best Practices for Using Polymorphism
- Use common method names across related classes.
- Design for interfaces, not for concrete implementations.
- Avoid tight coupling — polymorphism helps decouple your code.
- Use abstraction when needed for a cleaner structure.
- Follow naming conventions to make polymorphic code easier to understand.
Polymorphism is a powerful tool in Python’s object-oriented programming. It allows your code to be more flexible, maintainable, and scalable. By letting different classes implement the same methods in different ways, you can handle a wide variety of situations with minimal code changes.
Whether you’re working on small scripts or large software projects, understanding and using polymorphism effectively will make you a much better Python developer.