Description of type constructor parameters in Python: python Constructor
The test code is as follows:
Copy codeThe Code is as follows:
Class ModelMetaClass (type ):
Def _ new _ (cls, name, base, attrs ):
Logging.info ("cls is:" + str (cls ))
Logging.info ("name is:" + str (name ))
Logging.info ("base is:" + str (base ))
Logging.info ("attrs is:" + str (attrs ))
Return type. _ new _ (cls, name, base, attrs)
Pass
Pass
Class Model (dict ):
_ Metaclass _ = ModelMetaClass
Def _ init _ (self ):
Pass
Pass
Def main ():
M = Model ()
If _ name _ = '_ main __':
Main ()
Test results:
Copy codeThe Code is as follows:
INFO: root: cls is: <class '_ main _. ModelMetaClass'>
INFO: root: name is: Model
INFO: root: base is :( <type 'dict '> ,)
INFO: root: attrs is: {'_ module _': '_ main _', '_ metaclass __': <class '_ main __. modelMetaClass '>,' _ init _ ': <function _ init _ at 0x025C76F0>}
The conclusion is obvious. Cls is the name of the current class, that is, the class itself. Name refers to the class that points to ModelMetaClass through the _ metaclass _ attribute, that is, the class to be instantiated. ModelMetaClass intercepts the class instantiation. Base is the base class of the class to be instantiated. Attrs is a set of attributes of the class to be instantiated.