According to the "Principle of Everything" in Python, all created objects are the result of a known class instantiation, and which class is instantiated by? First look at the following piece of code
Class Foo (object): passobj = Foo () print type (obj) print type (Foo) result is: <class ' __main__. Foo ' ><type ' type ' >
As you can see: obj is instantiated by foo, and Foo is created by the type class
So foo can do this, look at the following:
def func: print "charles" Foo = Type (' Foo ', (object,), {' Func ': func}) Print foof = foo () F.func () The result is: <class ' _ _main__. Foo ' >charles
So the question arises, since class is instantiated by type, how does the class implement the creation of classes?
In fact, in a class instantiated by type, there is a __metaclass__ field that indicates who is instantiated to get the class
Class MyType (Type): def __init__ (self, What, Bases=none, dict=none): print "333" super (MyType, self). __ Init__ (What, bases, Dict) def __call__ (self, *args, **kwargs): print "444" obj = self.__new__ (self, *args, * *kwargs) self.__init__ (obj) class Foo (object): __metaclass__ = MyType def __init__ (self): print ' 222 ' def __new__ (CLS, *args, **kwargs): print ' 1111 ' return object.__new__ (CLS, *args, **kwargs) obj = Foo () The result is: 3334441111222
You can see that both MyType and Foo have __new__ () methods, so what does this method do?
__new__ () function:
See below:
Class A (object): def __init__ (self): print ' init ' def __new__ (CLS, *args, **kwargs): print "first " print object.__new__ (Cls,*args,**kwargs) A () Result: first<__main__. A Object at 0x0000000002730eb8>
You can see that the __new__ method executes first than the __init__ method, object.__new__ (Cls,*args,**kwargs) is an instantiated object of a, if I print self
Class A (object): def __init__ (self): print self, '----' print ' init ' def __new__ (CLS, *args, **kwargs) : print "First" print object.__new__ (Cls,*args,**kwargs) return object.__new__ (Cls,*args,**kwargs) A () The result is: first<__main__. A object at 0x000000000260be10><__main__. A object at 0x000000000260be10>----init
You can see the value of self, which is exactly the result of __new__ return;
Observe the object returned by __new__, which contains the CLS, which represents the current class, and if it is not the current class (even the parent class), __init__ will not be called:
Class B (object): passclass A (b): def __init__ (self): print self, '----' print ' init ' def __new_ _ (CLS, *args, **kwargs): print "First" return object.__new__ (B,*args,**kwargs) A () result is: first<__main__. A Object at 0x000000000238beb8>
Conclusion: 1, examples of Python classes are created by the __new__ method.
2. The __new__ method must have a CLS parameter that represents the current class, which is called only if the current cls,__init__ is passed in.
3, the class instance self, is the __new__ method return object.__new__ (Cls,*args,**kwargs) the result;
Reference: http://www.cnblogs.com/tuzkee/p/3540293.html
Http://www.cnblogs.com/wupeiqi/p/4766801.html
The underlying implementation of Python classes and objects