Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

An abstract method is a method declared within a base class that enforces a strict contract, requiring all concrete subclasses to provide their own implementation. In Python, abstract methods are defined using the @abstractmethod decorator from the built-in abc (Abstract Base Classes) module.
from abc import ABC, abstractmethod

class AbstractBase(ABC):
    @abstractmethod
    def enforced_method(self, data: str) -> str:
        """Method signature that subclasses must implement."""
        pass

class ConcreteSubclass(AbstractBase):
    def enforced_method(self, data: str) -> str:
        return f"Processed: {data}"

Technical Mechanics and Rules

  • Instantiation Blocking via Metaclass: The @abstractmethod decorator simply sets an __isabstractmethod__ attribute to True on the function object. By itself, it does not prevent class instantiation; a standard class containing an @abstractmethod can be instantiated without raising an error. Instantiation blocking is strictly enforced by the ABCMeta metaclass (typically applied by inheriting from abc.ABC). The metaclass reads the __isabstractmethod__ flags and populates the class’s __abstractmethods__ frozenset. The Python interpreter raises a TypeError only if an attempt is made to instantiate a class where __abstractmethods__ is not empty.
  • Subclass Enforcement: If a subclass inherits from an abstract base class but fails to override all abstract methods, the subclass implicitly remains abstract (its __abstractmethods__ set remains non-empty). It will raise a TypeError upon instantiation.
  • Base Implementations: Unlike abstract methods in some statically typed languages, Python’s @abstractmethod can contain an actual implementation. Subclasses are still strictly required to override the method, but they can invoke the parent class’s implementation using super().
from abc import ABC, abstractmethod

class BaseWithImplementation(ABC):
    @abstractmethod
    def process(self):
        return "Base processing steps."

class ValidSubclass(BaseWithImplementation):
    def process(self):
        base_result = super().process()
        return f"{base_result} Plus subclass steps."

Interaction with Other Decorators

The placement of @abstractmethod relative to other decorators depends on the type of decorator being used:
  1. Method Descriptors: When combining @abstractmethod with built-in method descriptors (@property, @classmethod, or @staticmethod), @abstractmethod must always be applied as the innermost decorator (closest to the def statement).
  2. Standard Function Decorators: When using standard custom function decorators, placing @abstractmethod innermost will cause the wrapper function to hide the __isabstractmethod__ flag, silently breaking the abstract enforcement. To maintain enforcement, custom decorators must use @functools.wraps to propagate the flag, or @abstractmethod must be placed as the outermost decorator.
from abc import ABC, abstractmethod
from functools import wraps

def custom_decorator(func):
    @wraps(func)  # Propagates __isabstractmethod__ to the wrapper
    def wrapper(*args, **kwargs):
        return func(*args, **kwargs)
    return wrapper

class ComplexAbstractBase(ABC):
    @property
    @abstractmethod
    def required_property(self):
        pass

    @classmethod
    @abstractmethod
    def required_classmethod(cls):
        pass

    @custom_decorator
    @abstractmethod
    def required_custom_decorated_method(self):
        pass
Master Python with Deep Grasping Methodology!Learn More