What Is a Metaclass?
Metaclasses create objects and are classes of classes.
Python metaclass
Right!
- Chinese name
- Metaclass
- Foreign name
- MetaClass
- 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
- Metaclasses create objects and are classes of classes.
- 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
MyClass = MetaClass () MyObject = MyClass () MyClass = type ('MyClass', (), ())
- Function type is actually a metaclass. In Python, including int, str, function and class, all are objects.