Questions
19 of 19
1How would you make a custom class usable in a for loop?
2How do __enter__ and __exit__ work together to implement the context manager protocol?
3How does Python implement inheritance, and what is the Method Resolution Order (MRO)?
4What are dataclasses, and how do they reduce boilerplate compared to manually writing __init__, __eq__, and __repr__?
5What is the @property decorator, and what problem does it solve compared to plain getter/setter methods?
6What is the difference between method overriding and method overloading, and why doesn't Python natively support overloading?
7What problem does the diamond inheritance pattern cause, and how does Python's MRO resolve it?
8What is the difference between a class and an instance in Python?
9How does Python implement encapsulation given that it has no true 'private' access modifiers?
10What is the purpose of self in instance methods, and why must it be explicitly declared in Python?
11What is the difference between __str__ and __repr__, and what convention should __repr__ follow?
12What is a metaclass, and what is type's relationship to every class in Python?
13What is the purpose of __eq__ and __hash__, and why must they be implemented consistently?
14What is a descriptor, and how do @property and ORMs like Django's models rely on the descriptor protocol?
15What is the difference between an instance method, a class method, and a static method?
16What is composition, and why is 'favor composition over inheritance' often recommended?
17What are dunder (magic) methods, and how do they enable operator overloading?
18When would you use a metaclass instead of a class decorator or __init_subclass__?
19How do abstract base classes (abc.ABC) enforce interface contracts in Python?
19 / 19

How do abstract base classes (abc.ABC) enforce interface contracts in Python?

Difficulty: 8/10
Abstract Base Classes, ABC, Interfaces

ABC and @abstractmethod block instantiation until every abstract method is implemented

abc.ABC is a class with ABCMeta as its metaclass. Methods decorated with @abstractmethod are recorded on the class, and ABCMeta refuses to instantiate any class that still has unimplemented abstract methods. That gives you a real interface contract: you get a TypeError at instantiation time, not a silent AttributeError later. ABCs also support virtual subclassing through register(), which lets a class be considered a subclass without inheritance. Note that abstractmethod does not prevent the method from having an implementation: subclasses can call super().method() to reuse shared behavior, and abstract properties and classmethods are also supported.

  1. 1

    Use @abstractmethod for methods subclasses must implement; ABCMeta enforces instantiation rules.

  2. 2

    Use @abstractproperty or property + abstractmethod for abstract attributes.

  3. 3

    Use register() or subclasshook for virtual subclasses when you cannot modify the target class.

  4. 4

    Trade-off: ABCs add runtime checks and a metaclass, which conflicts with other metaclasses. typing.Protocol offers structural typing as a lighter alternative.

  5. 5

    Common mistake: expecting @abstractmethod to prevent calling the method. It does not; it only prevents instantiation of incomplete subclasses.

  6. 6

    Common mistake: forgetting that an abstract class can still define concrete methods that subclasses inherit.

  7. 7

    Version note: abc.ABC has been available since 3.4. typing.Protocol with @runtime_checkable has been available since 3.8 for structural checks.

Scenario Questions

0-2 years experience

  1. 1You define an abstract class and try to instantiate it. What error appears and why?
  2. 2What does @abstractmethod do to a method?

2-5 years experience

  1. 1You want to reuse shared logic in an abstract method. How do subclasses call the parent implementation?
  2. 2You have a third-party class you cannot modify but want to treat it as a subclass of your ABC. How?

5-8 years experience

  1. 1You are designing a plugin interface. Would you use ABC, Protocol, or a plain base class with NotImplementedError?
  2. 2Your ABC uses a custom metaclass that conflicts with another base class. How do you resolve it?

8+ years experience

  1. 1Design an interface library that supports both nominal and structural typing, remains runtime-checkable, and does not impose metaclass conflicts.
  2. 2Explain how ABCMeta, __subclasshook__, and register() interact with isinstance and issubclass in large framework codebases.

Follow-up Questions

  • What is the difference between abc.ABC and typing.Protocol?
  • Why does instantiating a subclass with an unimplemented abstract method raise TypeError?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.