Class Origin and metaclass, origin metaclass

Source: Internet
Author: User

Class Origin and metaclass, origin metaclass
I. Overview

We know that classes can be used as instances to produce objects. How is the class itself produced? Let's trace the origin of the class.

Ii. Class Origin 2.1 create a class
Class Foo (object): def _ init _ (self, name): self. name = namef = Foo ('bigberg ') # create a Foo class # instantiate an object f

In python, everything is an object. If everything is an object theory: the obj object is created by executing the Foo class constructor, the Foo class object should also be created by executing the constructor of a class.

Print (type (f) print (type (Foo) # output <class '_ main __. foo '> <class 'type'> # object f is created by class Foo # Foo class is created by type

So,The f object is an instance of the Foo class.,The Foo class object is an instance of the type class.That is, the Foo class object is created through the type class constructor.

2.2 type creation class

Format of the type creation class. Class Name = type ('class name', (parent class,), {'method name': Method Memory Address })

Def func (self): # define a function print ('hello, World') Foo = type ('foo', (object,), {'tal': func }) # type: Creates a class. (object,) is the tuple f = Foo () f. talk () print (type (f) print (type (Foo) # output hello, world <class '_ main __. foo '> <class 'type'

We can see that type can indeed create a class and instantiate the object.

How can we transmit parameters? We need to write the constructor:

Def func (self): print ('hello, % s' % self. name) def _ init _ (self, name): # constructor self. name = nameFoo = type ('foo', (object,), {'tal': func, '_ init _': _ init __}) f = Foo ('bigberg ') # pass the parameter f. talk () # output hello, Bigberg

 So: classes are produced by type class instantiation.

  

3. _ new _ Method

_ New _ () is a new method in the new class. It can be understood in this way before constructing an instance, in Python, the constructor _ init _ () in the class is responsible for instantiating the class. Before _ init _ () is started, __new _ () determines whether to use the _ init _ () method, because _ new __() you can call the constructor of other classes or directly return other objects as instances of this class.

Class Foo (object): def _ init _ (self, name): self. name = name print ("in the Foo _ init _") def _ new _ (cls, * args, ** kwargs): # the first parameter cls, is the class currently being instantiated. Here is the object print ("Foo _ new _", cls, * args, ** kwargs) return object. _ new _ (cls) # inherit the _ new _ method f = Foo ('bigberg ') print (f. name) # output Foo _ new _ <class '_ main __. foo '> bigberg # the new method runs in the Foo _ init _ bigberg before the init method.

Features of the _ new _ () method:

  • The _ new _ () method is called when the class is ready to instantiate itself.
  • The _ new _ () method is always a static method of the class, even if it is not added with a static method decorator.

 

Note:

In fact, if the (new) class does not overwrite the _ new _ () method, that is, when the new class is not redefined _ new, by default, Python calls the _ new _ () method of the direct parent class of the class to construct the instance of the class. If the parent class of the class does not overwrite _ new __(), in this way, the _ new _ () method of the object will be traced back to this rule, because the object is the base class of all new classes.

If the _ new _ () method is not returned, the object cannot be instantiated.

Class Foo (object): def _ init _ (self, name): self. name = name print ("in the Foo _ init _") def _ new _ (cls, * args, ** kwargs ): object print ("Foo _ new _", cls, * args, ** kwargs) # return object. _ new _ (cls) f = Foo ('bigberg ') print (f. name) # output File "G:/python/untitled/study6/class origin. py ", line 39, in <module> print (f. name) AttributeError: 'nonetype 'object has no attribute 'name'

  

Iv. Functions of the _ metaclass _ method 4.1 metaclass

Metaclass is called a metaclass attribute. It is used to indicate who helped the class to be instantiated and created. To put it bluntly, it is equivalent to customizing a class by yourself.

1 class MyType (type): 2 3 def _ init _ (self, * args, ** kwargs): 4 5 print ("Mytype _ init __", * args, ** kwargs) 6 7 def _ call _ (self, * args, ** kwargs): 8 print ("Mytype _ call __", * args, ** kwargs) 9 obj = self. _ new _ (self) 10 print ("obj", obj, * args, ** kwargs) 11 print (self) 12 self. _ init _ (obj, * args, ** kwargs) 13 return obj14 15 def _ new _ (cls, * args, ** kwargs ): 16 print ("Mytype _ new _", * args, ** kwargs) 17 return type. _ new _ (cls, * args, ** kwargs) 18 19 print ('here... ') 20 21 22 class Foo (object, metaclass = MyType): 23 24 def _ init _ (self, name): 25 self. name = name26 27 print ("Foo _ init _") 28 29 def _ new _ (cls, * args, ** kwargs ): 30 print ("Foo _ new _", cls, * args, ** kwargs) 31 return object. _ new _ (cls) 32 33 f = Foo ("Bigberg") 34 print ("f", f) 35 print ("fname", f. name) 36 37 38 # output 39 40 here... 41 Mytype _ new _ Foo (<class 'object'>,) {'_ qualname _': 'foo', '_ init __': <function Foo. _ init _ at 0x000002A1968FE8C8>, '_ module _': '_ main _', '_ new _': <function Foo. _ new _ at 0x000002A1968FE950 >}42 Mytype _ init _ Foo (<class 'object'>,) {'_ qualname _': 'foo ', '_ init _': <function Foo. _ init _ at 0x000002A1968FE8C8>, '_ module _': '_ main _', '_ new _': <function Foo. _ new _ at 0x000002A1968FE950 >}43 Mytype _ call _ Bigberg44 Foo _ new _ <class '_ main __. foo '> 45 obj <__ main __. foo object at 0x000002A196905898> Bigberg46 <class '_ main __. foo '> 47 Foo _ init _ 48 f <__ main __. foo object at 0x000002A196905898> 49 fname Bigberg
View Code

The creation process is as follows:

4.2 execution sequence

Class generation call sequence is _ new _ --> _ init _ --> _ call __

Related Article

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.