Home »
Python
Python Metaclasses
Last Updated : May 02, 2025
Metaclasses are classes of classes that define how classes behave.
Python Metaclass
A metaclass is the class of a class. A class defines how an instance of a class i.e.; an object behaves whilst a metaclass defines how a class behaves. A class is an instance of a metaclass.
Role of Metaclasses
A metaclass is most commonly used as a class factory. While an object is created by calling the class, Python creates a new class (when it executes the class statement) by calling the metaclass. Combined with the __init__ and __new__ methods, metaclasses allow implementing 'additional things' when creating a class, like replacing the class with something else entirely.
The Class Creation Process
When the class statement is executed, Python first executes the body of the class statement as a normal block of code. The resulting namespace (a dict) holds the attributes of the class-to-be. The metaclass is determined by looking at the base classes of the class-to-be (metaclasses are inherited), at the __metaclass__ attribute of the class-to-be (if any), or the __metaclass__ global variable. The metaclass is then called with the name, bases, and attributes of the class to instantiate it.
Use-cases of metaclasses
The use-cases of metaclasses can be numerous, like
- Logging and profiling
- Dynamically adding new methods
- Dynamic resource locking/synchronization
- Registering the class at creation
Defining Python Metaclasses
Metaclasses are defined just like any other class, but they inherit from 'type'. The metaclass is automatically invoked when the class statement using metaclass ends. If a metaclass keyword is used on the other hand, the class assigned to it will be invoked instead of 'type',
Example
In this example, we are defining a custom metaclass MyMeta
that overrides the __new__
method to display information about the class being created, and then using it to create the class test2
, demonstrating how metaclasses control class creation:
class MyMeta(type):
def __new__(cls, clsname, superclasses, attrdict):
print("class name:{}".format(clsname))
print("super class name{}".format(superclasses))
print("attribute dict{}".format(attrdict))
return type.__new__(cls, clsname, superclasses, attrdict)
# Main Code
class test1:
pass
class test2(test1, metaclass=MyMeta):
pass
The output of the above code would be:
(Observe that we see that MyMeta__new__ is invoked and not from 'type')
class name:test2
super class name(,)
attribute dict{'__module__': '__main__', '__qualname__': 'test2'}
Create Singletons using Metaclass
The singleton pattern is a design pattern that puts a constraint in the instantiation of a class to only one object. Singleton design pattern is used in cases where exactly one object is required.
Example
In this example, we are creating a custom metaclass MySingleton
to implement the Singleton design pattern, ensuring that only one instance of a class exists by overriding the __call__
method and storing instances in a dictionary.
# Singleton Class
class MySingleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(MySingleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
# Main code
class SingletonClass(metaclass=MySingleton):
pass
class RegularClass:
pass
# Testing
test1 = SingletonClass()
test2 = SingletonClass()
print(test1 == test2)
test1 = RegularClass()
test2 = RegularClass()
print(test1 == test2)
The output of the above code would be:
True
False
Python Metaclasses Exercise
Select the correct option to complete each statement about Python metaclasses.
- In Python, a ___ is a class of a class that defines how a class behaves.
- The ___ function is the default metaclass in Python.
- To specify a metaclass in a class definition, use the ___ keyword argument.
Advertisement
Advertisement