Metaclass meta-class parsing

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.