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.
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.
- Python2 vs Python3 | Syntax and performance Comparison
- Automate the Conversion from Python2 to Python3
- Python VLC Instance - Creating MediaListPlayer Instance
- Python VLC Instance - Creating MediaList Instance
- Python VLC Instance - Creating new Media Instance
- Python VLC Instance - Creating MediaPlayer Instance
- Different Input and Output Techniques in Python3
- Python3 Intermediate Level Topics
- How to implement Dictionary with Python3?
- Python3 for GUI application | An Overview
- How to write Comments in Python3?
- Convert String to Double in Python3
- Introduction to Python3
- Python3 - if , if..else, Nested if, if-elif statements
- Why PyPy3 is preffered over Python3?
- New '=' Operator in Python3.8 f-string
- Positional-only Parameter in Python3.8
- Itertools in Python3
- Setting python3 as Default in Linux
- How to print spaces in Python3?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.