Code A:
# -*- coding:gb2312 -*-
class Dog (object):
def __init__(self):
print("-----init方法-----")
def __del__(self):
print("-----del方法-----")
def __str__(self):
#print("-----str方法-----")
return ("-----str方法-----")
def __new__(cls):
print("-----new方法-----")
#return object.__new__(cls)
xtq = Dog()
Code A running result:
Code B:
# -*- coding:gb2312 -*-
class Dog (object):
def __init__(self):
print("-----init方法-----")
def __del__(self):
print("-----del方法-----")
def __str__(self):
#print("-----str方法-----")
return ("-----str方法-----")
def __new__(cls):
print("-----new方法-----")
object.__new__(cls)
xtq = Dog()
Code B running result:
Code C:
# -*- coding:gb2312 -*-
class Dog (object):
def __init__(self):
print("-----init方法-----")
def __del__(self):
print("-----del方法-----")
def __str__(self):
#print("-----str方法-----")
return ("-----str方法-----")
def __new__(cls):
print("-----new方法-----")
return object.__new__(cls)
xtq = Dog()
Code C running result:
My question one:
Why do these three pieces of code A, B, and C produce different output results? What is the principle? Especially code B and code C, why does code C add return on the basis of B, and the result is one more init method than B.
My question two:
What is the difference between the two parameters self and cls when passing parameters to the method? Why does the __new__ method require cls as the parameter and the __init__ method requires self?
First of all, we must understand one thing: the difference between
self
andcls
,cls
representsthis class
,slef
is used to representinstances of this class
, if you understand this, you will succeed A little bit.The function parameters with
Theself
can be understood as this function, which is a method of the instance and is bound to the instance.__new__
method is used by new-style classes to create instances. Thecls
passed in are the parameters used to create instances ofobject.__new__
. Ifcls
is not passed in,object
will not be used at all. Know what kind of instance to create.Combined with the above, let’s talk about the three reasons why the output is different:
Why only --new? Because every class must call this
__new__
method to create an instance when instantiating an object, so it will definitely be called, but because this function has been overridden by you, so It just prints the--new method
, and does not return the created instance and puts it back, so__del__
will not happen eitherWhy are there only --new and --del, as mentioned in point 1, but here
__new___
does create a new instance, but it does not return, because only after returning can the object proceed to the next step__init__
, because there is only creation here and no return, so the result is like thisIf you understand the first two points, I believe there should be no problem with this, because it is created and returned, so '__init__' is also executed, and everything happens as normal behavior
Finally, I want to explain: Why
__del__
will be executed. Theoretically, this will only be executed when the instance is destructed bydel
. There is no code similar todel xtq
. Why will it be executed? The reason is , the program is over and is about to exit. When the lower-level program exits,python
spontaneously performs memory recycling, so everything returns to dust and the created objects are also destructed one by one