Metaclasses create objects and are classes of classes.
Python metaclass
Right!
Metaclasses create objects and are classes of classes.
- Name
- Metaclass
- 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 .
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
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.