Compare and contrast 'Built-in Types' with 'Abstract Base Classes' (ABCs). How do ABCs enforce system boundaries in a large-scale plugin architecture?
Python interview question for Advanced practice.
Answer
Built-in types (like list, dict) define the core data structures. Abstract Base Classes (ABCs) define the Interface or contract that a type must follow. Production Enforcement: In a plugin-based system (e.g., a data processor that supports multiple input formats), you define an ABC (e.g. DataSource). By using the @abstractmethod decorator, Python prevents the instantiation of any subclass that hasn't implemented the required methods. Virtual Subclassing: ABCs also support 'Virtual Subclassing' via register(). You can take an existing class (like a third-party library's connection object) and register it as a subclass of your ABC. This allows isinstance(obj, Plugin) to return True even if the developer of the third-party library never heard of your Plugin class. This provides a powerful way to enforce system boundaries and type safety across diverse codebases without rigid inheritance hierarchies.
Explanation
You can use 'issubclass(list, Iterable)' to check if a type implements a specific interface without it explicitly inheriting from it.