Open In App

How to create an instance of a Metaclass that run on both Python2 and Python3?

Last Updated : 01 Oct, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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.  

Python 3

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.
 

Python3




class MetaCls(type):
  def __new__(cls, name, bases, attrs):
    return super(MetaCls, cls).__new__(cls, name, bases, attrs)
    
class C(object, metaclass=MetaCls):
  pass
  
print('Type of class C:',type(C))


Output

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.

Python 2

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:

Python




class MetaCls(type):
  def __new__(cls, name, bases, attrs):
    return super(MetaCls, cls).__new__(cls, name, bases, attrs)
    
class C(object):
  __metaclass__ = MetaCls
    
print(type(C))


Output

<class '__main__.MetaCls'>

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. 

  1. By creating a stand-in class
  2. 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.

Python3




import six
  
class MetaCls(type):
  def __new__(cls, name, bases, attrs):
    return super(MetaCls, cls).__new__(cls, name, bases, attrs)
    
class C(six.with_metaclass(MetaCls)):
  pass
  
print(type(C))


Output

<class '__main__.Meta'>

Now, let’s see how to create an instance of metaclass by using a decorator. 

Python3




import six
  
class MetaCls(type):
  def __new__(cls, name, bases, attrs):
    return super(MetaCls, cls).__new__(cls, name, bases, attrs)
    
@six.add_metaclass(MetaCls)
class C(object):
  pass
  
print(type(C))


Output

<class '__main__.MetaCls'>


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads