Is the _ _ init__ method necessary to call in Python?

like the following code:

class Test(object):
    __instance = None

    def __new__(cls, *args, **kwargs):
        if cls.__instance:
            return cls.__instance
        else:
            return object.__new__(cls)

    def __init__(self, value):
        self.value = value
        print("__init__")
        Singletone.__instance = self

sl = Test(100)
sl2 = Test(300)

print(sl.value)
print(sl2.value)

I guess the output is:

__init__
100
100

however, the actual output is:

__init__
__init__
300
300

Why?

Mar.15,2021

Yes, even if _ _ new__ returns the generated object, _ _ init__ is bound to be executed;
the related code is as follows (python2.7)

  

is inevitable.

Menu