Questions
18 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?
18 / 19

When would you use a metaclass instead of a class decorator or __init_subclass__?

Difficulty: 9/10
Metaclasses, Init Subclass, Class Decorators

Prefer init_subclass or decorators; reach for a metaclass only for inheritance-wide or deep control

The PEP 487 guidance is clear: init_subclass should cover most cases where you previously needed a metaclass, because it is a normal classmethod hook that runs on every subclass. Class decorators are even simpler and apply to a single class. Metaclasses remain the right choice when you need to control the class namespace before the class object exists, customize prepare, interact with the metaclass itself (for example subclassing ABCMeta), or affect every class in a hierarchy in a way that must be part of type identity. If you find yourself writing a metaclass only to register subclasses or validate methods, you are probably over-engineering.

  1. 1

    init_subclass: best for per-subclass validation, registration, and hooking into class creation without changing type identity.

  2. 2

    Class decorator: best for a single class transformation, easy to apply and remove, plays well with type checkers.

  3. 3

    Metaclass: best when you must customize the class namespace via prepare, or when your feature must be part of the class's type.

  4. 4

    Trade-off: metaclasses compose poorly. Combining two metaclasses requires a shared subclass, which is intrusive for library consumers.

  5. 5

    Common mistake: using a metaclass for singleton, registry, or attribute validation, all of which init_subclass or descriptors handle more cleanly.

  6. 6

    Version note: init_subclass and set_name were added in 3.6 precisely to reduce metaclass usage. PEP 487 documents the intent.

javascript

Scenario Questions

0-2 years experience

  1. 1You want every subclass to register itself. Which mechanism do you choose?
  2. 2What does a class decorator receive and what does it return?

2-5 years experience

  1. 1You need to reject subclasses that fail to define a required method. Metaclass or __init_subclass__?
  2. 2You need to apply a transformation to a single class only. Which tool is simplest?

5-8 years experience

  1. 1You are writing a public library and your metaclass conflicts with ABCMeta. How do you redesign the API?
  2. 2You need custom class namespace behavior like ordered attributes. Which hook is required?

8+ years experience

  1. 1Design a plugin framework that is compatible with ABCMeta, dataclasses, and user-provided base classes, avoiding metaclass conflicts.
  2. 2Compare the cost of metaclass-based validation versus __init_subclass__ and decorators on class creation time, inheritance behavior, and tooling compatibility.

Follow-up Questions

  • What does __prepare__ let a metaclass do that __init_subclass__ cannot?
  • How do class decorators interact with static type checkers compared to metaclasses?
Share

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