# Metaclasses

> What are metaclasses in Python? When would you use them?

Canonical: https://www.internhack.in/learn/interview/python-interview/python-metaclasses
Last updated: 2026-11-28

Difficulty: Advanced

## Question

What are metaclasses in Python? When would you use them?

## Answer

A metaclass is the class of a class. Just as a class defines how instances behave, a metaclass defines how classes behave.

In Python, 'type' is the default metaclass. When you write 'class Foo:', Python calls type('Foo', bases, namespace) to create the class.

Metaclass use cases:

- Enforcing class contracts (required methods/attributes)
- Automatic registration of subclasses
- Modifying class creation (adding methods, changing attributes)
- ORM field mapping (Django models, SQLAlchemy)
- Singleton pattern

Modern alternatives: \__init_subclass_\_ (Python 3.6+) and class decorators handle most metaclass use cases more simply.

## Code examples

### Understanding type() as a Metaclass

```
# Everything is an object, and every object has a type
print(type(42))          # <class 'int'>
print(type('hello'))     # <class 'str'>
print(type(int))         # <class 'type'>
print(type(type))        # <class 'type'> (type is its own metaclass)

# Classes are instances of type
class MyClass:
    x = 10
    def greet(self):
        return 'hello'

# The above is equivalent to:
MyClass2 = type('MyClass2', (), {'x': 10, 'greet': lambda self: 'hello'})

print(type(MyClass))     # <class 'type'>
print(MyClass.x)         # 10
print(MyClass2.x)        # 10

# Inheritance
class Child(MyClass):
    pass

Child2 = type('Child2', (MyClass,), {})
print(Child2().greet())  # 'hello'
```

type() is both a function (returns type of object) and a class (the metaclass of all classes). All classes are instances of type.

### Custom Metaclass

```
# Metaclass that enforces required methods
class InterfaceMeta(type):
    def __new__(mcs, name, bases, namespace):
        cls = super().__new__(mcs, name, bases, namespace)
        
        # Skip check for the base class itself
        if bases:  # Only check subclasses
            required = getattr(cls, '_required_methods', [])
            for method in required:
                if method not in namespace:
                    raise TypeError(
                        f"{name} must implement {method}()"
                    )
        return cls

class Plugin(metaclass=InterfaceMeta):
    _required_methods = ['execute', 'cleanup']

# This works:
class GoodPlugin(Plugin):
    def execute(self):
        return 'executing'
    def cleanup(self):
        return 'cleaning'

print(GoodPlugin().execute())  # 'executing'

# This raises TypeError at class definition time:
# class BadPlugin(Plugin):
#     def execute(self):
#         return 'executing'
#     # Missing cleanup()!
# TypeError: BadPlugin must implement cleanup()
```

The metaclass \__new_\_ is called when the CLASS is created (not when instances are created). This allows validating the class definition itself.

### \__init_subclass_\_: Modern Alternative

```
# __init_subclass__ (Python 3.6+) - simpler than metaclasses
class Plugin:
    _registry = {}
    
    def __init_subclass__(cls, plugin_name=None, kwargs):
        super().__init_subclass__(kwargs)
        name = plugin_name or cls.__name__.lower()
        Plugin._registry[name] = cls
        print(f"Registered plugin: {name}")

class JsonPlugin(Plugin, plugin_name='json'):
    def process(self):
        return 'Processing JSON'

class CsvPlugin(Plugin, plugin_name='csv'):
    def process(self):
        return 'Processing CSV'

class XmlPlugin(Plugin):  # Uses class name as default
    def process(self):
        return 'Processing XML'

print(Plugin._registry)
# {'json': <class 'JsonPlugin'>, 'csv': <class 'CsvPlugin'>, 'xmlplugin': <class 'XmlPlugin'>}

# Factory pattern using registry
def get_plugin(name):
    cls = Plugin._registry.get(name)
    if cls:
        return cls()
    raise ValueError(f"Unknown plugin: {name}")

plugin = get_plugin('json')
print(plugin.process())  # 'Processing JSON'
```

\__init_subclass_\_ is called every time a class is subclassed. It handles 90% of metaclass use cases (registration, validation) without the complexity.

## Key points

- Tim Peters: 'Metaclasses are deeper magic than 99% of users should ever worry about'
- Prefer \__init_subclass_\_ (3.6+) or class decorators over metaclasses in most cases
- Django models and SQLAlchemy declarative base both use metaclasses internally

## Common follow-up questions

- What is the difference between \__new_\_ and \__init_\_ in a metaclass?
- How does Django's Model metaclass work?
- When would you use a metaclass vs \__init_subclass_\_ vs a class decorator?

## Interview tips

- Admit metaclasses are rarely needed - show you know when NOT to use them
- Explain the type() duality (function and metaclass) clearly
- Mention \__init_subclass_\_ as the modern, simpler alternative

## Concepts covered

Metaclasses, type, \__new_\_, \__init_subclass_\_, Class Creation

Previous: [Itertools & Functools](https://www.internhack.in/learn/interview/python-interview/python-itertools-functools) · Next: [Memory Management & GC](https://www.internhack.in/learn/interview/python-interview/python-memory-gc) · [All of Python Interview](https://www.internhack.in/learn/interview/python-interview)

## More Python Interview questions

- [Virtual Environments & pip](https://www.internhack.in/learn/interview/python-interview/python-venv-pip)
- [Type Hints & mypy](https://www.internhack.in/learn/interview/python-interview/python-type-hints)
- [Testing (pytest)](https://www.internhack.in/learn/interview/python-interview/python-testing)
- [Async/Await (asyncio)](https://www.internhack.in/learn/interview/python-interview/python-asyncio)
- [Python Design Patterns](https://www.internhack.in/learn/interview/python-interview/python-design-patterns)
- [Python vs Other Languages](https://www.internhack.in/learn/interview/python-interview/python-vs-others)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [Python Interview](https://www.internhack.in/learn/interview/python-interview)
