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

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 between self and cls, cls represents this class, slef is used to represent instances of this class, if you understand this, you will succeed A little bit.

The function parameters with self can 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. The cls passed in are the parameters used to create instances of object.__new__. If cls 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:

  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 by del. There is no code similar to del 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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template