Under The Hood
backend python CPython 3.12+

Metaclasses: type, __new__, __init_subclass__, and Dynamic Class Generation

Last updated
Prerequisites:
Python Classes and OOP
Python Data Model
  • python
  • metaclasses
  • type
  • init_subclass
  • metaprogramming
  • class-creation

Read at your depth

The practical view

In Python, `type` is both the default metaclass and a function. You can dynamically create a class using `type(name, bases, dict)`: `MyClass = type('MyClass', (object,), {'x': 10})`. To customize class creation, define a class inheriting from `type`: `class Meta(type): def __new__(cls, name, bases, dct): ...`. In modern Python (3.6+), you can often use `__init_subclass__` instead of full metaclasses for validating or registering subclasses.

The same idea in other frameworks

python equivalent
`class Meta(type): def __new__(cls, name, bases, dct): ...`.

Legacy vs modern

Heavy Metaclass vs Modern __init_subclass__ Hook

Metaclasses add complexity and conflict risks; `__init_subclass__` provides simple subclass validation without metaclass overhead.

before → after
Complex Custom Metaclass
class ValidateMeta(type): def __new__(cls, name, bases, dct): ... // ❌ Metaclass conflict risk
Modern __init_subclass__ Hook
class BaseModel: def __init_subclass__(cls, **kwargs): ... // ✅ NEW: Clean, no metaclass conflicts

Interview gotchas

Context

Interviewers test your deep understanding of multiple inheritance and metaclass resolution.

The mechanical answer

A metaclass conflict occurs when a class inherits from multiple base classes whose metaclasses are not subclasses of one another (e.g. Class A has `MetaA`, Class B has `MetaB`). When defining `class C(A, B):`, Python cannot determine which metaclass to use and throws `TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases`. The fix is to create a common derived metaclass: `class MetaC(MetaA, MetaB): pass`.

Trap

Prefer `__init_subclass__` or class decorators to avoid metaclass conflicts in public libraries.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.