Python ABC Module
Python ABC Module Interview Questions
What is the ABC module in Python?
The abc module provides infrastructure for defining Abstract Base Classes (ABCs). ABCs are classes that cannot be instantiated directly and serve as blueprints for other classes, enforcing method implementation in subclasses.
What are the main components of the abc module?
Main components: ABC (base class), @abstractmethod, @abstractclassmethod, @abstractstaticmethod, ABCMeta (metaclass), and register() method for virtual subclasses.
How to create abstract properties using ABC?
Use @property with @abstractmethod decorators. Both getter and setter can be marked as abstract if needed.
What is the difference between ABC and regular class with NotImplementedError?
ABC prevents instantiation at class definition time (compile-time check), while NotImplementedError raises error at method call time (runtime check). ABC provides earlier error detection.
How to check if a class is abstract?
Use inspect.isabstract() or check __abstractmethods__ attribute. A class is abstract if __abstractmethods__ is not empty.
Can abstract methods have implementations?
Yes, abstract methods can have implementations. Subclasses can call them via super(). This is called the "Template Method Pattern".
What is the register() method in ABC?
register() allows registering a class as a "virtual subclass" without actual inheritance, supporting duck typing. The class is considered a subclass for isinstance() and issubclass() checks.
How to create abstract class methods?
Use @abstractmethod with @classmethod decorators. Decorator order matters: @classmethod should be outermost.
How to create abstract static methods?
Use @abstractmethod with @staticmethod decorators. Like class methods, @staticmethod should be the outermost decorator.
What is ABCMeta metaclass and when to use it?
ABCMeta is the metaclass for ABCs. Use it when creating an ABC without inheriting from ABC class, or for complex metaclass hierarchies. The ABC class itself uses ABCMeta as its metaclass.
How to use ABC with multiple inheritance?
ABC classes work normally with multiple inheritance. Subclasses must implement all abstract methods from all parent ABCs. Python's MRO (Method Resolution Order) handles the inheritance hierarchy.
How does __subclasshook__ work in ABC?
__subclasshook__ allows custom logic for determining if a class is a subclass, supporting structural subtyping. It enables duck typing by checking for method presence rather than inheritance.
What are the advantages of using ABC over duck typing?
Advantages: Early error detection, explicit interface definition, better documentation, IDE support, type checking compatibility, and formal contracts between classes.
How to use ABC with type hints?
ABCs integrate well with Python's type hinting system. Use them as type annotations to ensure objects have required methods. Works with tools like mypy for static type checking.
Can we dynamically add abstract methods to a class?
Yes, but it's complex and not recommended. It involves modifying __abstractmethods__ and using ABCMeta metaclass methods. Generally avoid this in production code.
What is the difference between ABC and Protocol?
ABC uses nominal typing (requires explicit inheritance), while Protocol uses structural typing (based on method presence). ABC enforces inheritance; Protocol supports duck typing with type checking.
How to create interface-like classes with ABC?
Create ABCs with only abstract methods (no concrete implementations). This defines pure interfaces that subclasses must implement fully, similar to interfaces in other languages.
What are common use cases for ABC in real projects?
Common uses: Plugin systems, repository pattern, service interfaces, strategy pattern, template method pattern, factory methods, testing mocks, and API design.
How does ABC help with testing and mocking?
ABCs provide clear interfaces that can be easily mocked. Use spec parameter with Mock to ensure mocks match the ABC interface. Enables dependency injection and easier unit testing.
What are best practices when using the abc module?
Best practices: Use for defining contracts, clear naming conventions, document abstract methods, combine with type hints, provide default implementations when appropriate, keep hierarchies shallow, and test abstract classes.
Note: The ABC module provides compile-time checks for interface implementation, helping create clear APIs and catch errors early. While Python supports duck typing, ABCs offer explicit interface definitions for better code organization and reliability.