Content · Programming · Python

Python abc.abstractmethod Decorator: Usage and Examples

Mark methods that concrete subclasses must implement with @abstractmethod.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

Mark methods that concrete subclasses must implement with @abstractmethod.

How It Works


abc.abstractmethod sets the abstract marker inspected by ABCMeta. The decorator works for instance methods as well as properly ordered classmethods, staticmethods, and properties.

A class remains abstract until all inherited abstract requirements are implemented.

Step-by-Step Example


from abc import ABC, abstractmethod

class Serializer(ABC):
    @abstractmethod
    def serialize(self, value):
        pass

class StringSerializer(Serializer):
    def serialize(self, value):
        return str(value)

print(StringSerializer().serialize(42))

Understanding the Example


  • The class groups related state and methods so each instance can manage its own values.
  • The function definition gives the operation a reusable name and makes its inputs explicit.

A successful run of this example prints the following output:

42

Where This Fits in a Real Program


A class is useful when data and the operations that preserve its rules belong together. Distinguish class-level attributes from per-instance state, and make invalid operations fail clearly. Inheritance is not always needed: simpler composition or a standalone function may be easier to maintain.

Practice: Try It Yourself


Add a required deserialize() method and implement it in a subclass.

For this exercise, predict the result before running the changed code. Then explain the difference between the original and modified cases using the actual values, not just whether the program completed.

Further Reading


Read the official Python documentation for complete parameters, return types and exceptions.

Common Beginner Mistakes


  • Expecting the base class decorator to implement a method automatically.
  • Combining abstractmethod with other decorators in the wrong order.

Key Takeaways


  • Mark methods that concrete subclasses must implement with @abstractmethod.
  • abc.abstractmethod sets the abstract marker inspected by ABCMeta.
  • A class remains abstract until all inherited abstract requirements are implemented.
Share this resourceSend this page to someone who may find it useful.