Creating Custom Metaclasses
In Python, we can customize the class creation process by passing the metaclass keyword in the class definition. This can also be done by inheriting a class that has already passed in this keyword.
We can see below that the type of MyMeta class is type and that the type of MyClass and MySubClass is MyMeta.
When defining a class and no metaclass is defined the default type metaclass will be used. If a metaclass is given and it is not an instance of type(), then it is used directly as the metaclass.
__new__ and __init__
Metaclasses can also be defined in one of the two ways shown below. We'll explain the difference between them below.
class MetaOne(type):
def __new__(cls, name, bases, dict):
pass
class MetaTwo(type):
def __init__(self, name, bases, dict):
pass
__new__ is used when one wants to define dict or bases tuples before the class is created. The return value of __new__is usually an instance of cls. __new__ allows subclasses of immutable types to customize instance creation. It can be overridden in custom metaclasses to customize class creation. __init__ is usually called after the object has been created so as to initialize it.
Metaclass __call__ method
According to the official docs, we can also override other class methods by defining a custom __call__() method in the metaclass that allows custom behavior when the class is called.
Metaclass __prepare__ method
According to Python's data model docs
Once the appropriate metaclass has been identified, then the class namespace is prepared. If the metaclass has a __prepare__ attribute, it is called as namespace = metaclass.__prepare__(name, bases, **kwds) (where the additional keyword arguments, if any, come from the class definition). If the metaclass has no __prepare__attribute, then the class namespace is initialized as an empty ordered mapping. - docs.python.org
Singleton Design using a Metaclass
This is a design pattern that restricts the instantiation of a class to only one object. This could prove useful for example when designing a class to connect to the database. One might want to have just one instance of the connection class.
class SingletonMeta(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(SingletonMeta,cls).__call__(*args, **kwargs)
return cls._instances[cls]
class SingletonClass(metaclass=SingletonMeta):
pass
Conclusion
In this article, we have learned about what metaclasses are and how we can implement them in our Python programming. Metaclasses can be applied in logging, registration of classes at creation time and profiling among others. They seem to be quite abstract concepts, and you might be wondering if you need to use them at all. Long-term Pythonista, Tim Peters answers that question best.
"Metaclasses are deeper magic than 99% of users should ever worry about. If you wonder whether you need them, you don’t (the people who actually need them know with certainty that they need them, and don’t need an explanation about why).” - realpython
If you would like to learn more about Python, start DataCamp's Python Programming skill track and check out our Python Classes Tutorial.
A metaclass in Python is a class of a class that defines how a class behaves. A class is itself an instance of a metaclass. A class in Python defines how the instance of the class will behave. In order to understand metaclasses well, one needs to have prior experience working with Python classes. Before we dive deeper into metaclasses, let's get a few concepts out of the way.
Everything in Python is an Object
Python Classes can be Created Dynamically
typein Python enables us to find the type of an object. We can proceed to check the type of object we created above.Wait, What just happened? We'd expect the type of the object we created above to be class, but it's not. Hold on to that thought. We will cover it further in a few. We also notice that the type of
typeitself istype. It is an instance oftype. Another magical thing thattypedoes is enable us to create classes dynamically. Let's show how we'd do that below. TheDataCampclass shown below would be created as shown below usingtype:In the above example
DataCampis the class name whileDataCampClassis the variable that holds the class reference. When usingtypewe can pass attributes of the class using a dictionary as shown below:In case we wanted our
PythonClassto inherit from theDataCampclass we pass it to our second argument when defining the class usingtypeNow that those two concepts are out of the way, we realize that Python creates the classes using a metaclass. We have seen that everything in Python is an object, these objects are created by metaclasses. Whenever we call
classto create a class, there is a metaclass that does the magic of creating the class behind the scenes. We've already seentypedo this in practice above. It is similar tostrthat creates strings andintthat creates integers. In Python, the___class__attribute enables us to check the type of the current instance. Let's create a string below and check its type.We can also check the type using
type(article).When we check the type of
str, we also find out that it's type.When we check the type for
float,int,list,tuple, anddict, we will have a similar output. This is because all of these objects are of typetype.We've already seen
typecreates classes. Hence when we check the__class__of__class__it should returntype.