How to create an instance of a Metaclass that run on both Python2 and Python3?
Metaclasses are classes that generate other classes. It is an efficient tool for class verification, to prevent sub class from inheriting certain class function, and dynamic generation of classes. Here we will discuss how to create an instance of a Metaclass that runs on both Python 2 and Python 3. Before delving into it, let’s go through the code design for each Python version.
In Python 3, an instance of a metaclass is created while declaring a class, by providing the Metaclass to the metaclass keyword argument. Let’s look into the preferred way of doing this in Python 3.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Type of class C: <class '__main__.MetaCls'>
Here, we have created a class called C by providing the MetaCls class to the metaclass keyword argument. You can see the type of class C as MetaCls.
Note: MetaCls is a metaclass that directly inherited from type.
Now, let look into, how to create an instance of a metaclass in Python 2. In Python 3, we have seen that the metaclass is mentioned in the keyword argument during class declaration; whereas in Python 2, a metaclass is assigned in the class body by using a __metaclass__ attribute. Let’s see the code below:
Metaclass code that runs on both Python 2 and Python 3
Now it’s clear how the syntax differs in Python 2 and Python 3. This difference in syntax may create trouble if you are trying to migrate your Python 2 code to Python 3. Since Python 3 is backward incompatible, the Python 3 syntax doesn’t execute on Python 2 and vice versa.
Here comes the importance of a tool called six. It provides two ways to declare a metaclass that ensures cross-compatibility.
- By creating a stand-in class
- As a decorator
In the first method, we will create a stand-in class and use it as a direct subclass. Let’s look into the below code.
Now, let’s see how to create an instance of metaclass by using a decorator.