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

后端 未结 7 1353
-上瘾入骨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:53

    In Python, calling the super-class' __init__ is optional. If you call it, it is then also optional whether to use the super identifier, or whether to explicitly name the super class:

    object.__init__(self)
    

    In case of object, calling the super method is not strictly necessary, since the super method is empty. Same for __del__.

    On the other hand, for __new__, you should indeed call the super method, and use its return as the newly-created object - unless you explicitly want to return something different.

    0 讨论(0)
提交回复
热议问题