Python is an object-oriented language that simplifies working with classes. A class in Python defines specific behaviors for its instances, which are objects in Python. These class instances are created using the class as a blueprint. Similarly, a metaclass in Python describes behaviors for its instances, which are classes. A metaclass serves as the blueprint for the class itself, similar to how a class serves as the blueprint for instances of that class. The default metaclass is "type," and all metaclasses must inherit from the "type" metaclass.
A metaclass serves primarily as a class factory. When you instantiate an object by invoking the class, Python generates a new class by utilizing the metaclass. By complementing the standard init and new methods, metaclasses offer the capability to incorporate additional functionalities during class creation, such as registering the freshly created class within a registry or substituting the class with an entirely different entity.
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 exercise control over the construction and setup of classes within the metaclass, you can implement the metaclass's new method and/or init constructor. In practice, many metaclasses tend to override only one of these methods. Similar to a regular class, a metaclass possesses a new method responsible for generating the class instance and an init method that personalizes the instance. Additionally, if you intend to return an entity other than a freshly established class of the specific type, you must utilize new.
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
The prepare method is invoked prior to executing the class body, and its role is to yield a dictionary-like object which serves as the local namespace for the entirety of the code within the class body. When the metaclass has a prepare attribute, it's invoked with the arguments namespace = metaclass.prepare(name, bases, **kwds). Conversely, in cases where the metaclass lacks a prepare attribute, the class namespace gets initialized as an empty ordered mapping. This feature was introduced in Python 3.0.
Creating custom Metaclass
The primary objective of a metaclass is to automatically modify a class during its creation. This practice is typically applied to APIs, where the goal is to generate classes that align with the present context. To craft a custom metaclass, it should inherit from the type metaclass and commonly override:custom metaclass:
And then we can use it
Python metaclasses are a powerful feature that allows you to control the creation and behavior of classes. They provide a way to customize class creation and can be used to implement dynamic behaviors that operate at the class level, offering advanced capabilities beyond traditional class inheritance.