Copy codeThe Code is as follows:
# Coding: UTF-8
"""
_ New _ and _ init _ are the same thing. Let's look at the following code:
If the _ new _ method is not defined for a class, the _ new _ method is inherited from the parent class.
_ New _ run before _ init _. This occurs when the class is called with parentheses,
First, call the _ new _ method of the class and put back the Instance Object of the class. This instance object is the first parameter of the _ init _ method.
In the code, the memory addresses of tmp, self, and p are the same, and they are all instance objects of the class.
"""
Class Foo (object ):
Def _ new _ (cls, * args, ** kwargs ):
"If the _ new _ method is not overwritten, that is, if the _ new _ method is not written, the class will
Inherit the _ new _ method to complete the returned value Instance Object
"""
Print "_ new _ method called first"
Tmp = super (Foo, cls). _ new _ (cls, * args, ** kwargs)
Print id (tmp)
Print type (tmp)
Print isinstance (tmp, Foo)
Print issubclass (type (tmp), Foo)
Return tmp
Def _ init _ (self ):
"Self is the default value passed by python. This value is the return value of the call _ new """
Print "_ init _ called"
Print id (self)
P = Foo ()
Print id (p)
Print type (p)