Python is an object-oriented language that makes working with classes simple and easy. A class in python is a way to describe a specific behaviour for its instances, which are python objects . These objects of that class are created using the class as the blueprint . A metaclass in python is a way to describe a specific behaviour for its instances, which are python classes. A metaclass is the blueprint of the class itself, just like a class is the blueprint for instances of that class. The default metaclass is type, and all metaclasses must derive from type .
A metaclass is mostly used as a class-factory. When you create an object by calling the class, Python creates a new class by calling the metaclass. Combined with the normal __init__ and __new__ methods, metaclasses therefore allow you to do extra things when creating a class, like registering the new class with some registry or replace the class with something else entirely.
There are numerous use cases for metaclasses . Here are few:
- Automatic property creation
- Automatically adding new methods
- Registering classes at creation time
- Logging and profiling
- Interface checking
Metaclass's __new__() and __init__()
- __new__(): It's a method which is called before __init__(). It creates the object and return it. We can overide this method to control how the objects are created.
- __init__(): This method just initialize the created object passed as parameter.
Metaclasses can be defined in one of the two ways shown below.
To control the creation and initialization of the class in the metaclass , you can implement the metaclass's __new__ method and/or __init__ constructor. Most real-life metaclasses will probably override just one of them. The metaclass behaves just like a class in that it has a __new__ which creates the instance of the class and an __init__ which customizes the instance. Also, you have to use __new__ if you want to return something other than a newly created class of the type in question.
When to use Metaclasses?
In practice, it's rare that someone will have to use metaclasses. Generally speaking, you will have to rely on metaclasses when you need a dynamic behaviour that cannot be described at the object level, but rather at the class level.
A Simple metaclass
We can use type directly to make a class, without any class statement. It can be called in following ways:
- When called with only one argument, it returns the type.
- When called with three parameters, it creates a class. Following arguments are passed to it:
- Class name
- Tuple having base classes inherited by class
- Class Dictionary
It's because the function type is in fact a metaclass. type is the metaclass Python uses to create all classes behind the scenes.
The class statement isn't just syntactic sugar, it does some extra things, like setting an adequate __qualname__ and __doc__ properties or calling __prepare__ .
The __prepare__ method
This method is called before the class body is executed and it must return a dictionary-like object that's used as the local namespace for all the code from the class body. If the metaclass has a __prepare__ attribute, it is called as namespace = metaclass.__prepare__(name, bases, **kwds). If the metaclass has no __prepare__attribute, then the class namespace is initialized as an empty ordered mapping. It was added in Python 3.0 .
Creating custom Metaclass
The main purpose of a metaclass is to change the class automatically, when it's created. Normally do this for APIs, where you want to create classes matching the current context. In orderto create your custom metaclass, it have to inherit type metaclass and usually override:
And then we can use it