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

Metaclasses are a concept in object-oriented programming where a class is an instance of another class, known as the metaclass. They allow for the customization of class creation and behaviour, enabling the creation of classes with specific attributes and methods. A metaclass is the blueprint of the class itself, just like a class is the blueprint for instances of that class.

Python supports metaclasses, which create custom classes with unique behaviour. Since Python 2 and Python 3 have different syntaxes for metaclasses, we need compatible approaches to create metaclasses that work across both versions.

Understanding Metaclass Syntax

A basic metaclass inherits from type and can override methods like __new__ and __init__ ?

class MyNewClass(type):
    def __init__(cls, name, bases, dict):
        pass

In this syntax, name represents the class name, bases defines a tuple of base classes, and dict contains the class namespace dictionary.

Method 1: Using six.with_metaclass()

The six library provides with_metaclass() function for Python 2/3 compatibility ?

from six import with_metaclass

class MyMetaClass(type):
    def __new__(cls, name, bases, attrs):
        print(f"Creating class: {name}")
        return super(MyMetaClass, cls).__new__(cls, name, bases, attrs)

class MyClass(with_metaclass(MyMetaClass)):
    value = 42

print(f"Type of MyClass: {type(MyClass)}")
print(f"MyClass.value: {MyClass.value}")
Creating class: MyClass
Type of MyClass: <class '__main__.MyMetaClass'>
MyClass.value: 42

Method 2: Version-Specific Handling

You can check Python version and handle differences accordingly using six.PY2 ?

import six

class VersionAwareMetaClass(type):
    def __new__(cls, name, bases, attrs):
        if six.PY2:
            print("Running on Python 2")
        else:
            print("Running on Python 3")
        
        # Add a custom attribute to all classes
        attrs['python_version'] = '2' if six.PY2 else '3'
        return super(VersionAwareMetaClass, cls).__new__(cls, name, bases, attrs)

class MyClass(six.with_metaclass(VersionAwareMetaClass)):
    pass

print(f"Type: {type(MyClass)}")
print(f"Python version: {MyClass.python_version}")
Running on Python 3
Type: <class '__main__.VersionAwareMetaClass'>
Python version: 3

Comparison of Methods

Method Compatibility Best For
six.with_metaclass() Python 2 & 3 Simple metaclass usage
Version checking with six.PY2 Python 2 & 3 Version-specific behavior

Conclusion

Use six.with_metaclass() for creating Python 2/3 compatible metaclasses. The six library handles version differences automatically, making your metaclass code portable across Python versions.

Updated on: 2026-03-27T06:31:01+05:30

324 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements