Should __init__() call the parent class's __init__()?

后端 未结 7 1385
-上瘾入骨i
-上瘾入骨i 2020-12-02 07:07

I\'m used that in Objective-C I\'ve got this construct:

- (void)init {
    if (self = [super init]) {
        // init class
    }
    return self;
}
<         


        
7条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-02 07:35

    In Anon's answer:
    "If you need something from super's __init__ to be done in addition to what is being done in the current class's __init__ , you must call it yourself, since that will not happen automatically"

    It's incredible: he is wording exactly the contrary of the principle of inheritance.


    It is not that "something from super's __init__ (...) will not happen automatically" , it is that it WOULD happen automatically, but it doesn't happen because the base-class' __init__ is overriden by the definition of the derived-clas __init__

    So then, WHY defining a derived_class' __init__ , since it overrides what is aimed at when someone resorts to inheritance ??

    It's because one needs to define something that is NOT done in the base-class' __init__ , and the only possibility to obtain that is to put its execution in a derived-class' __init__ function.
    In other words, one needs something in base-class' __init__ in addition to what would be automatically done in the base-classe' __init__ if this latter wasn't overriden.
    NOT the contrary.


    Then, the problem is that the desired instructions present in the base-class' __init__ are no more activated at the moment of instantiation. In order to offset this inactivation, something special is required: calling explicitly the base-class' __init__ , in order to KEEP , NOT TO ADD, the initialization performed by the base-class' __init__ . That's exactly what is said in the official doc:

    An overriding method in a derived class may in fact want to extend rather than simply replace the base class method of the same name. There is a simple way to call the base class method directly: just call BaseClassName.methodname(self, arguments).
    http://docs.python.org/tutorial/classes.html#inheritance

    That's all the story:

    • when the aim is to KEEP the initialization performed by the base-class, that is pure inheritance, nothing special is needed, one must just avoid to define an __init__ function in the derived class

    • when the aim is to REPLACE the initialization performed by the base-class, __init__ must be defined in the derived-class

    • when the aim is to ADD processes to the initialization performed by the base-class, a derived-class' __init__ must be defined , comprising an explicit call to the base-class __init__


    What I feel astonishing in the post of Anon is not only that he expresses the contrary of the inheritance theory, but that there have been 5 guys passing by that upvoted without turning a hair, and moreover there have been nobody to react in 2 years in a thread whose interesting subject must be read relatively often.

提交回复
热议问题