Description of type constructor parameters in Python
This article mainly introduces the description of the type constructor parameters in Python. This article uses an encoding example to explain the role and meaning of the Python type parameters. For more information, see
The test code is as follows:
The 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:
The Code is as follows:
INFO: root: cls is:
INFO: root: name is: Model
INFO: root: base is :( ,)
INFO: root: attrs is: {'_ module _': '_ main _', '_ metaclass __': , '_ Init __': }
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.