Study Notes
Introduction
Polymorphism is one of the fundamental principles of object‑oriented programming. The
word polymorphism comes from Greek words meaning 'many forms'. In programming, it
refers to the ability of a single function, method, or operator to behave differently
depending on the context in which it is used.
In Python, polymorphism allows developers to write flexible and reusable code. Instead of
creating many different methods for different data types, programmers can define a single
interface that works with multiple object types.
This concept is widely used in modern software development because it helps reduce code
duplication and improves readability. By applying polymorphism, developers can design
programs that are easier to extend and maintain.
Definition
Polymorphism in Python refers to the ability of different objects to respond to the same
method or function call in different ways.
It allows a single function name or operator to perform multiple tasks depending on the
type of object it is applied to.
In object‑oriented programming, polymorphism is often implemented through method
overriding, operator overloading, or the use of common interfaces across multiple classes.
Types of Polymorphism
Python mainly supports two major types of polymorphism: compile‑time polymorphism
and runtime polymorphism.
Compile‑time polymorphism usually appears in languages that support method
overloading, where multiple methods share the same name but differ in parameters.
Runtime polymorphism occurs when a child class overrides a method defined in its parent
class and the method executed depends on the object used during execution.
In Python, runtime polymorphism is more common and is achieved using method
overriding and dynamic typing.
, Polymorphism with Built‑ in Functions
Python demonstrates polymorphism even in its built‑in functions. For example, the len()
function can operate on different data types such as strings, lists, and tuples.
Although the function name is the same, its behavior changes depending on the object
passed as an argument.
This ability to work with different types of objects using the same interface is a clear
demonstration of polymorphism.
Example – Built‑ in Function Polymorphism
Example code demonstrating polymorphism with the len() function:
string_example = 'Python Programming'
print(len(string_example))
list_example = [10,20,30,40]
print(len(list_example))
tuple_example = (1,2,3,4,5)
print(len(tuple_example))
In each case the same function name len() is used, but the output depends on the object
type.
Polymorphism with Class Methods
Polymorphism is commonly used with class methods where different classes define the
same method name but perform different actions.
This allows programmers to use a consistent interface while implementing class‑specific
behavior.
For example, different shapes such as Circle and Rectangle may each implement a method
named area() that calculates area differently.
Example – Polymorphism Using Classes
Example Python code:
class Dog:
def speak(self):
print('Dog barks')