Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
Polymorphism is an important concept in object-oriented programming. It allows objects of different classes to be treated as objects of a common superclass, enabling you to write more flexible and reusable code. Here's a step-by-step tutorial on polymorphism in Python:
First, create a base class that will serve as the common superclass for other classes. In this example, we create a Shape
class with a calculate_area
method:
class Shape: def calculate_area(self): pass
Now, create subclasses that inherit from the base class and override the calculate_area
method to provide their own implementations. In this example, we create Circle
and Rectangle
subclasses:
class Circle(Shape): def __init__(self, radius): self.radius = radius def calculate_area(self): return 3.14 * self.radius ** 2 class Rectangle(Shape): def __init__(self, width, height): self.width = width self.height = height def calculate_area(self): return self.width * self.height
With polymorphism, you can treat objects of different classes as if they were objects of the common superclass. This allows you to write more flexible and reusable code. In this example, we create a function called display_area
that takes a Shape
object and calls its calculate_area
method:
def display_area(shape): area = shape.calculate_area() print(f"The area of the shape is {area}") circle = Circle(5) rectangle = Rectangle(3, 4) display_area(circle) # Output: The area of the shape is 78.5 display_area(rectangle) # Output: The area of the shape is 12
The display_area
function works with any object that inherits from the Shape
class, regardless of its actual class. This is an example of polymorphism in action.
Python provides a module called abc
(Abstract Base Classes) that allows you to define abstract base classes with abstract methods. Subclasses must override these abstract methods, ensuring they provide the required interface. Here's an example with the Shape
class:
from abc import ABC, abstractmethod class Shape(ABC): @abstractmethod def calculate_area(self): pass
Now, if a subclass doesn't override the calculate_area
method, a TypeError
will be raised when trying to create an object of that subclass.
This tutorial covered the basics of polymorphism in Python, how to create base classes and subclasses, and how to use polymorphism to treat objects of different classes uniformly. Understanding polymorphism is essential for writing flexible and reusable code in object-oriented programming.
Using polymorphism for code flexibility in Python:
class Shape: 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 Rectangle(Shape): def __init__(self, length, width): self.length = length self.width = width def area(self): return self.length * self.width # Using polymorphism for code flexibility shapes = [Circle(5), Rectangle(4, 6)] for shape in shapes: print(f"Area: {shape.area()}")
Implementing polymorphism with interfaces in Python:
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 Rectangle(Shape): def __init__(self, length, width): self.length = length self.width = width def area(self): return self.length * self.width # Implementing polymorphism with interfaces shapes = [Circle(5), Rectangle(4, 6)] for shape in shapes: print(f"Area: {shape.area()}")