Recommended + Favorites: Deep understanding of the meta-class in Python (Metaclass)
Do some notes to learn to learn:
In most programming languages, a class is a piece of code that describes how to generate an object, and in Python a class is an object that has the ability to create an object (class instance) itself.
Because the essence of it is an object:
- It can be assigned to a variable
- You can copy it.
- Add Property
- Passing as a parameter
- You can create them dynamically at run time, you can create classes in functions, just use the class keyword to
The Python interpreter automatically creates this object when using the class keyword, and Python also provides a way to handle it manually: type ()
Type is a class factory that generates class objects, and is actually a class, and the class that specializes in building class objects is called a meta-class:
The Help for viewing type is as follows:
Init Signature:type (self,/, *args, **kwargs) docstring: type (object_or_name, bases, Dict) type (object), the Object ' s Typetype (name, bases, Dict), a new typetype: type
1. The first example, when passing in an object, returns the type information of the object, which is used to know what the type of an object is.
2. The second example, the information passed in the class object, generates a class object, name is the class name, bases is the inherited parent class, and Dict is the property dictionary, which is used to dynamically create the class
__metaclass__ Property
You can add a __metaclass__ property to the class, change the life syntax of the Meta class in Pep 3115, and specify the Metaclass keyword in the list of base classes, for example: Class Foo (Base1, Base2, metaclass= Mymeta):p
Class Foo (object): __metaclass__=something
When the Interpreter parses class Foo (object) and the object Foo is not created, Python looks for the __metaclass__ attribute in the class definition, creates it, and, if found, Python uses it to create Foo, without using the type.
Class Foo (Bar): Pass
If Foo does not have the __metaclass__ property, it continues recursively in the parent bar to look for the __metaclass__ property, attempting the previous action.
The following types.py in the source code, PEP 3115 to make changes, provides a new_class with Metaclass to dynamically create a class object, where the _calculate_meta function is used to calculate the most derived extended meta-class:
def _calculate_meta (meta, bases): "" "Calculate the most derived metaclass. " " Winner = Meta for base in bases: Base_meta = Type (base) if Issubclass (winner, Base_meta): continue If Issubclass (Base_meta, winner): winner = Base_meta Continue # Else: raise TypeError ("Metaclass Conflict: " the metaclass of a derived class" "must be A (non-strict) subclass" "of the metaclasses of a ll its bases ") return winner
From the above code, we can see that each base class of the class object is judged compared to find the most extended meta-class (winner).
In the case of the meta-classes themselves, they are actually simple:
- Block creation of classes
- Modifying a class
- Returns the class after the modification
Meta-classes in Python (metaclass)