What Is a Metaclass?

Metaclasses create objects and are classes of classes.

Python metaclass

Right!
Metaclasses create objects and are classes of classes.
Chinese name
Metaclass
Foreign name
MetaClass
Before understanding metaclasses, you need to understand Python classes. Python borrows some concepts from the Smalltalk language. In Python, a class is an object. As long as you use the keyword class, Python executes it and creates an object.
  >>> class ObjectCreator (object):


 ...


 pass ...


 >>> my_object = ObjectCreator ()


 >>> print (my_object)


 <__main __. ObjectCreator object at 0x8974f2c>

This object (class) itself is capable of being created (instance), which is why it is a class object . [1]
therefore
  • You can assign it to a variable
  • You can copy it
  • You can add attributes to it
  • You can take it as a parameter of the function [2]
    Metaclass
  MyClass = MetaClass ()
 MyObject = MyClass ()
 MyClass = type ('MyClass', (), ())

Function type is actually a metaclass. In Python, including int, str, function and class, all are objects.

IN OTHER LANGUAGES

Was this article helpful? Thanks for the feedback Thanks for the feedback

How can we help? How can we help?