The word “meta” means self-referential. A metaclass is a class whose instances are classes. Like an “ordinary” class defines the
Objects are instances of Classes and the Classes are instances of Metaclasses. We create objects from classes and then we create classes out of metaclasses. In Python,
type is the default metaclass for all the classes but this can be customized as we need.
The term metaprogramming refers to the potential for a program to have knowledge of or manipulate itself. Python supports a form of metaprogramming for classes called metaclasses.
Old-Style vs. New-Style Classes
With old-style classes, class and type are not quite the same
obj.__class__ designates the class,
New-style classes unify the concepts of class and type.
obj is an instance of a new-style class,
type(obj) is the same as
Type and Class
In Python 3, all classes are new-style classes. Thus, in Python 3 it is reasonable to refer to an object’s type and its class interchangeably.
In Python 2, classes are old-style by default. Prior to Python 2.2, new-style classes weren’t supported at all. From Python 2.2 onward, they can be created but must be explicitly declared as new-style.
>>> class Foo: ... pass ... >>> x = Foo() >>> type(x) <class '__main__.Foo'> >>> type(Foo) <class 'type'>
But the type
type is a metaclass, of which classes are instances. Just as an ordinary object is an instance of a class, any new-style class in Python, and thus any class in Python 3, is an instance of the
In the above case:
xis an instance of
Foois an instance of
typeis also an instance of
metaclass, so it is an instance of itself.