abc Module

abc stands for Abstract Base Classes β€” it’s a built-in Python module used to define abstract classes and methods.


πŸ“¦ What is ABC?

  • ABC is a class from the abc module.

  • You inherit from ABC when you want to create an abstract class.

  • It makes sure that the class can't be used directly unless all abstract methods are implemented in the child class.


🧩 What is abstractmethod?

  • A decorator from the abc module.

  • Used to declare methods without a body.

  • It forces subclasses to implement that method.


βœ… Example:

from abc import ABC, abstractmethod

class Animal(ABC):  # abstract class
    @abstractmethod
    def speak(self):  # abstract method
        pass

You can't create an object of Animal Unless you define speak() In the child class.


πŸ” Summary:

  • abc β†’ module for abstract concepts

  • ABC β†’ base class to make a class abstract

  • abstractmethod β†’ to define a method without body

Updated on