Tutorial

Abstraction in Python

3 min read

Abstraction in Python is a fundamental concept that allows developers to create more modular and maintainable code. It is particularly relevant in object-oriented programming languages like Python.

In Python, abstraction is achieved through the use of abstract classes and interfaces.

Abstraction involves separating the essential features or behaviors of an object from the specific implementation details. It provides a high-level view of an object, focusing on what it does rather than how it does it.

This approach allows developers to work with complex systems by hiding unnecessary details and exposing only the relevant functionalities.

In Python, abstraction is often implemented using abstract classes, which cannot be instantiated directly.

An abstract class serves as a blueprint for other classes and provides a common interface for its subclasses. It defines a set of abstract methods that must be implemented by any concrete class inheriting from it. Abstract methods are declared without implementation and act as placeholders for the required functionality.

Abstract Class in Python

To create an abstract class in Python, we need to import the ABC (Abstract Base Class) module from the abc module and use the @abstractmethod decorator to mark the methods as abstract.

Here’s an example of using abstraction in Python

from abc import ABC, abstractmethod

class Shape(ABC):
    @abstractmethod
    def area(self):
        pass

    @abstractmethod
    def perimeter(self):
        pass

class Rectangle(Shape):
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return 2 * (self.length + self.width)

class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius

    def area(self):
        return 3.14 * self.radius**2

    def perimeter(self):
        return 2 * 3.14 * self.radius

# Creating objects of concrete classes
rectangle = Rectangle(5, 3)
circle = Circle(4)

# Calling methods on the objects
print("Rectangle area:", rectangle.area())
print("Rectangle perimeter:", rectangle.perimeter())

print("Circle area:", circle.area())
print("Circle perimeter:", circle.perimeter())

Code explanation of using abstraction in Python

In the above code example, we have a Python abstract class Shape that defines two abstract methods:

area() and perimeter()

Any class that inherits from Shape is required to implement these methods.

The Shape class acts as a blueprint for different shapes. We then have two concrete classes, Rectangle and Circle, which inherit from Shape and provide their own implementations for the abstract methods. Each class calculates the area and perimeter based on its specific shape.

By using abstraction, we can treat the Rectangle and Circle objects as instances of the abstract class Shape. This allows us to write generic code that works with any shape without worrying about the specific details of each shape’s implementation.

Note: the abc module is imported to make Shape an abstract base class (ABC). The abstractmethod decorator is used to mark the abstract methods, indicating that they must be overridden by subclasses. If a subclass does not implement these methods, a TypeError will be raised.

Python Abstraction helps in designing modular and extensible code by providing a clear separation between the interface and the implementation. It allows developers to focus on high-level concepts and promotes code reuse and maintainability.


Leave a Reply

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