[Python Newbie] Ask a question about the function of __new__ method
phpcn_u1582
phpcn_u1582 2017-06-28 09:24:55
0
1
577

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?

phpcn_u1582
phpcn_u1582

reply all (1)
过去多啦不再A梦

First of all, we must understand one thing: the difference betweenselfandcls,clsrepresentsthis class,slefis used to representinstances of this class, if you understand this, you will succeed A little bit.

The function parameters withselfcan be understood as this function, which is a method of the instance and is bound to the instance.

The

__new__method is used by new-style classes to create instances. Theclspassed in are the parameters used to create instances ofobject.__new__. Ifclsis not passed in,objectwill 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:

  1. 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 either

  2. Why 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 this

  3. If 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,pythonspontaneously performs memory recycling, so everything returns to dust and the created objects are also destructed one by one

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!