abc stands for Abstract Base Classes β itβs a built-in Python module used to define abstract classes and methods.
π¦ What is ABC?
-
ABCis a class from theabcmodule. -
You inherit from
ABCwhen 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
abcmodule. -
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