Python Abstraction

Tutorial on Python Abstraction

Welcome to codeswithpankaj.com! In this tutorial, we will delve into the concept of abstraction in Python. We'll cover what abstraction is, how it works in Python, and provide detailed examples to illustrate its application.

Table of Contents

  1. Introduction to Abstraction

  2. Why Use Abstraction?

  3. Abstract Classes

  4. Abstract Methods

  5. The abc Module

  6. Creating Abstract Classes and Methods

  7. Implementing Abstraction in Python

  8. Practical Examples

  9. Advantages of Abstraction

  10. Summary

1. Introduction to Abstraction

What is Abstraction?

Abstraction is an object-oriented programming concept that involves hiding the implementation details of a class and exposing only the necessary functionalities. It helps in reducing complexity and allows the programmer to focus on interactions at a higher level.

Key Points

  • Abstraction focuses on what an object does instead of how it does it.

  • It allows us to define interfaces without implementing them directly.

2. Why Use Abstraction?

  • Simplifies Code: Abstraction helps in managing complexity by breaking down the system into smaller, manageable parts.

  • Enhances Code Maintenance: It makes the code easier to maintain and extend.

  • Encourages Code Reusability: Abstraction allows for code reusability by defining common interfaces.

3. Abstract Classes

What is an Abstract Class?

An abstract class is a class that cannot be instantiated and is meant to be subclassed. It can contain abstract methods, which must be implemented by its subclasses.

from abc import ABC, abstractmethod

class Animal(ABC):
    @abstractmethod
    def sound(self):
        pass

4. Abstract Methods

What is an Abstract Method?

An abstract method is a method that is declared, but contains no implementation. Abstract methods are meant to be implemented by subclasses.

class Animal(ABC):
    @abstractmethod
    def sound(self):
        pass

class Dog(Animal):
    def sound(self):
        return "Bark"

dog = Dog()
print(dog.sound())

5. The abc Module

Introduction to the abc Module

The abc module in Python provides the infrastructure for defining abstract base classes. It is used to create abstract classes and methods.

from abc import ABC, abstractmethod

Key Components of the abc Module

  • ABC: A helper class that serves as a base class for defining abstract base classes.

  • abstractmethod: A decorator indicating that a method is abstract.

6. Creating Abstract Classes and Methods

Example: Creating an Abstract Class

from abc import ABC, abstractmethod

class Vehicle(ABC):
    @abstractmethod
    def start_engine(self):
        pass

Example: Implementing Abstract Methods

class Car(Vehicle):
    def start_engine(self):
        return "Car engine started"

car = Car()
print(car.start_engine())

7. Implementing Abstraction in Python

Example: Abstraction with Multiple Abstract Methods

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

    @abstractmethod
    def perimeter(self):
        pass

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

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

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

rect = Rectangle(10, 20)
print(f"Area: {rect.area()}")
print(f"Perimeter: {rect.perimeter()}")

8. Practical Examples

Example 1: Abstract Class for Different Payment Methods

class Payment(ABC):
    @abstractmethod
    def pay(self, amount):
        pass

class CreditCardPayment(Payment):
    def pay(self, amount):
        return f"Paid {amount} using Credit Card"

class PayPalPayment(Payment):
    def pay(self, amount):
        return f"Paid {amount} using PayPal"

payment = CreditCardPayment()
print(payment.pay(100))

payment = PayPalPayment()
print(payment.pay(200))

Example 2: Abstract Class for Different Types of Employees

class Employee(ABC):
    @abstractmethod
    def get_salary(self):
        pass

class FullTimeEmployee(Employee):
    def get_salary(self):
        return "Full-time salary"

class PartTimeEmployee(Employee):
    def get_salary(self):
        return "Part-time salary"

full_time = FullTimeEmployee()
part_time = PartTimeEmployee()

print(full_time.get_salary())
print(part_time.get_salary())

9. Advantages of Abstraction

  • Encapsulation: Abstraction helps in encapsulating the details and exposing only the necessary parts.

  • Reduction in Complexity: It reduces the complexity of the code by hiding the implementation details.

  • Improved Code Maintainability: It makes the code more maintainable and easier to understand.

10. Summary

In this tutorial, we explored the concept of abstraction in Python, its importance, and how to implement it using abstract classes and methods. We also covered practical examples to illustrate the application of abstraction. Abstraction is a powerful feature that enhances code flexibility, readability, and maintainability.

For more tutorials and in-depth explanations, visit codeswithpankaj.com!


This tutorial provides a comprehensive overview of Python abstraction, detailing each topic and subtopic with examples and explanations. For more such tutorials, keep following codeswithpankaj.com!

Last updated