Introduction
A Metaclass means the class of a class. In Python Classes are objects, which are instances of some Class.
As can see below every Class in Python are instance of a class type.
class Example:
pass
>>> type(Example)
<class 'type'>
typeis a MetaClass in Python which is responsible for classes creation.
Dynamic Class creation using type()
When
type()is called with one argument, it returns the argument type.When
type()is called with 3 arguments, a class is created.- Class name
- Tuple having base classes inherited by class
- Class dictionary populated with class methods and variables
# creating a base class
class Base:
def myfunc(self):
print("This is from Base!")
# Normal Way
class Test(Base):
x = 1
# Dynamic Way using type()
Test = type('Test', (Base, ), dict(x=1))
Creating Custom Metaclass
Let’s consider an example:
class Foo:
pass
>>> f = Foo()
The expression Foo() creates a new instance of class Foo and the following occurs:
The
__call__method of Foo’s parent class is called. Since Foo is a standard new-style class, its parent class is thetypemetaclass, so type’s__call__method is invoked.That
__call__method in turn invokes the following:__new____init__
To create a Custom MetaClass, the class has to inherit type and overide __new__, __init__ methods.

Notes
- Metaclasses can be used when the classes needs to change dynamically.
- Metaclasses can be used when a decorator needs to be applied to every function in a class.