python
metaclass

What are metaclasses in Python?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Metaclasses are the mechanism Python uses to create classes. Just as objects are created from classes, classes themselves are created from metaclasses, and the default metaclass is type.

A metaclass controls class creation

Most Python code never needs a custom metaclass because type already does the job. The key idea is that a class statement does not magically appear in memory; Python collects the class body, then asks the metaclass to build the final class object.

You can see that classes are instances of type:

python
1class User:
2    pass
3
4print(type(User))
5print(isinstance(User, type))

That is the starting point for understanding metaclasses. A custom metaclass is just a subclass of type that changes how class objects are constructed.

Why people use custom metaclasses

Metaclasses are useful when you want to enforce or automate behavior at class-definition time rather than instance-creation time. Common use cases include:

  • automatic class registration
  • validation of required class attributes
  • injecting methods or metadata
  • framework-level conventions

Many frameworks prefer metaclasses because the checks happen once when the class is declared, not later when someone instantiates it incorrectly.

Define a custom metaclass by subclassing type

The usual entry point is __new__, where you can inspect or modify the class namespace before the class is created.

python
1class RegistryMeta(type):
2    registry = {}
3
4    def __new__(mcls, name, bases, namespace):
5        cls = super().__new__(mcls, name, bases, namespace)
6        if name != "BasePlugin":
7            mcls.registry[name] = cls
8        return cls
9
10
11class BasePlugin(metaclass=RegistryMeta):
12    pass
13
14
15class CsvPlugin(BasePlugin):
16    pass
17
18
19class JsonPlugin(BasePlugin):
20    pass
21
22print(sorted(RegistryMeta.registry))

Here the metaclass automatically registers subclasses as soon as they are defined. No instance needs to be created for the registration to happen.

Metaclasses can enforce class-level rules

Because the metaclass runs during class creation, it is a natural place to reject invalid class definitions.

python
1class RequiresTableName(type):
2    def __new__(mcls, name, bases, namespace):
3        cls = super().__new__(mcls, name, bases, namespace)
4        if name != "ModelBase" and not hasattr(cls, "table_name"):
5            raise TypeError(f"{name} must define table_name")
6        return cls
7
8
9class ModelBase(metaclass=RequiresTableName):
10    pass
11
12
13class User(ModelBase):
14    table_name = "users"
15
16print(User.table_name)

This is the kind of behavior frameworks use for model registration, declarative configuration, and consistency checks.

Know when a simpler feature is better

Metaclasses are powerful, but they are not the first tool you should reach for. In many cases, a class decorator, __init_subclass__, or an ordinary factory function is easier to read and maintain.

For example, automatic subclass registration can often be written with __init_subclass__ instead of a metaclass. That is simpler because it keeps the logic close to the base class and avoids changing the metaclass hierarchy.

Use a metaclass when you genuinely need to control class creation itself or when you are working inside a framework that already builds around that mechanism.

Common Pitfalls

  • Using a metaclass when __init_subclass__ or a class decorator would be simpler.
  • Forgetting that metaclass logic runs when the class is defined, not when an instance is created.
  • Writing metaclass code that is hard to debug because it hides too much behavior at declaration time.
  • Creating metaclass conflicts by mixing incompatible base classes with different custom metaclasses.
  • Treating metaclasses as a normal application-level default instead of a specialized framework tool.

Summary

  • A metaclass is the object that creates classes, and the default metaclass in Python is type.
  • Custom metaclasses let you modify or validate classes at definition time.
  • They are useful for registration, enforcement, and framework conventions.
  • Many tasks that look like metaclass problems can be solved more simply with decorators or __init_subclass__.
  • Metaclasses are best used deliberately, not as a first-choice abstraction.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.