first, the process of creating a class
Second, what is the meta-class
The meta-class that inherits type in Python3
Example
#Way OneclassMyType (type):" "inheriting type is the Meta class" " def __init__(self,*args,**Kwargs):Print("objects created by MyType", self)#FooSuper (Mytype,self).__init__(*args,**Kwargs)def __call__(Self, *args, * *Kwargs): obj= Super (mytype,self).__call__(*args,**Kwargs)Print("class to create an object", Self,obj)#FooclassFoo (Object,metaclass=mytype):#object parentheses will execute the __call__ method, and the __call__ method inherits the type __call__ method, the __call__ method of type executes the __new__ method first, then executes the __init__ method. So, Foo is the user created with type="haha" Age= 18obj= Foo ()
#Mode twoclassMyType (type):def __init__(Self, *args, * *Kwargs):Print("ssss") Super (MyType, self).__init__(*args, * *Kwargs)def __call__(CLS, *args, * *Kwargs): v=dir (CLS) obj= Super (MyType, CLS).__call__(*args, * *Kwargs)returnobj#object parentheses will execute the __call__ method.classFoo (MyType ('ZCC', (object,), {})):#MyType (' Zcc ', (object,), {}) is equivalent to class ZCC (object):p the same as the one that creates a ZCC classuser ='Haiyan' Age= 18obj= Foo ()
#Way ThreeclassMyType (type):def __init__(Self, *args, * *Kwargs):Print("ssss") Super (MyType, self).__init__(*args, * *Kwargs)def __call__(CLS, *args, * *Kwargs): v=dir (CLS) obj= Super (MyType, CLS).__call__(*args, * *Kwargs)returnobj#object parentheses will execute the __call__ method.defWith_metaclass (arg,base):Print("Class Object", MyType ('ZCC', (base,), {})) returnArg'ZCC', (base,), {})#returns a Class object <class ' __main__. ZCC ' >classFoo (With_metaclass (mytype,object)):#MyType (' Zcc ', (object,), {}) is equivalent to class ZCC (object):p the same as the one that creates a ZCC classuser ='Haiyan' Age= 18obj= Foo ()
classASD (type):PassQQQ= ASD ("qwe", (object,), {})#Create a Class (Qwe, and inherit the object class) with the ASD element class#class ASD (qwe):#Passobj =QQQ ()#the class that can be created is a meta class#the class that can create the objectPrint(obj)#<__main__.qwe object at 0x00000000024ffba8>Print(obj.__class__)#<class ' __main__.qwe ' >Print(obj.__class__.__class__)#<class ' __main__. ASD ' >Print(obj.__class__.__class__.__class__)#<class ' type ' >Print(obj.__class__.__class__.__class__.__class__)#<class ' type ' >
other
Metaclass meta-class parsing