In Python, metclass is a class whose instances are classes. Metaclass are derived from type.

Metaclasses change the classes automatically when decorators are not efficient use metaclass.

Metaclass implements __new__ method which calls the parent class to create a new instance. The creation of metaclass includes the name of the class, class bases and attributes used in the class.

Metaclass are used to create class dyunamically using the type keyword. See following code snippet-

car = type("vehicle", "Vehicle", "{speed:50}")

Here the type accepts the name of the instance, then the baseclass name and attributes defined for the object.

See the below code to create your own metaclass.

Vehicle_Meta class inherits from type. Implements __new__ method which accepts four parameters. cls, name – name of the class to be dynamically created, bases – meta class base class and dict- attributes attached to the class. custom attribute speed is created.

Vehicle class refers to the meta class Vehicle_Meta.

When the __dict__ is accessed it has the custom attribute along with other attributes created as a part of Class.