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

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.

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__. OTOH, 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.

– Georg Schölly Sep 6 '09 at 14:22 2 In old-style classes, you could only call the super init if the super class actually had an init defined (which it often doesn't). Therefore, people typically think about calling super method, rather than doing it out of principle. – Martin v.

Löwis Sep 6 '09 at 14:47 If the syntax in python was as simple as super init, it would be more common. Just a speculative thought; the super construct in Python 2. X is a bit awkward to me.

– kaizer. Se Sep 6 '09 at 20:34 Here seems to be an interesting (and possibly contradicting) example: bytes.Com/topic/python/answers/… – mlvljr Sep 6 '09 at 12:16.

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. But if you don't need anything from super's __init__, no need to call it. Example: >>> class C(object): def __init__(self): self.

B = 1 >>> class D(C): def __init__(self): super(). __init__() self. A = 1 >>> class E(C): def __init__(self): self.

A = 1 >>> d = D() >>> d. A 1 >>> d. B # This works because of the call to super's init 1 >>> e = E() >>> e.

A 1 >>> e. B # This is going to fail since nothing in E initializes b... Traceback (most recent call last): File "", line 1, in e. B # This is going to fail since nothing in E initializes b... AttributeError: 'E' object has no attribute 'b' __del__ is the same way, (but be wary of relying on __del__ for finalization - consider doing it via the with statement instead).

I rarely use __new__. I do all the initialization in __init__.

The definition of class D(C) must be corrected like that super(D,self). __init__() – eyquem Aug 14 at 19:15.

Edit: (after the code change) There is no way for us to tell you whether you need or not to call your parent's __init__ (or any other function). Inheritance obviously would work without such call. It all depends on the logic of your code: for example, if all your __init__ is done in parent class, you can just skip child-class __init__ altogether.

Consider the following example: >>> class A: def __init__(self, val): self. A = val >>> class B(A): pass >>> class C(A): def __init__(self, val): A. __init__(self, val) self.

A += val >>> A(4). A 4 >>> B(5). A 5 >>> C(6).

A 12.

I removed the super call from my example, is I wanted to know if one should call the parent class's implementation of init or not. – Georg Schölly Sep 6 '09 at 14:14 you might want to edit the title then. But my answer still stands.

– SilentGhost Sep 6 '09 at 14:19.

There's no hard and fast rule. The documentation for a class should indicate whether subclasses should call the superclass method. Sometimes you want to completely replace superclass behaviour, and at other times augment it - i.e.

Call your own code before and/or after a superclass call. Update: The same basic logic applies to any method call. Constructors sometimes need special consideration (as they often set up state which determines behaviour) and destructors because they parallel constructors (e.g.In the allocation of resources, e.g. Database connections).

But the same might apply, say, to the render() method of a widget. Further update: What's the OPP? Do you mean OOP?

No - a subclass often needs to know something about the design of the superclass. Not the internal implementation details - but the basic contract that the superclass has with its clients (using classes). This does not violate OOP principles in any way.

That's why protected is a valid concept in OOP in general (though not, of course, in Python).

– Georg Schölly Sep 6 '09 at 14:16 You said that sometimes one would want to call own code before the superclass call. To do this, one needs knowledge of the parent class's implementation, which would violate the OPP. – Georg Schölly Sep 6 '09 at 16:34.

IMO, you should call it. If your superclass is object, you should not, but in other cases I think it is exceptional not to call it. As already answered by others, it is very convenient if your class doesn't even have to override __init__ itself, for example when it has no (additional) internal state to initialize.

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 instanciation.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).

docs.python.org/tutorial/classes.html#in... 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.

I was sure that this post was going to be upvoted. I'm afraid that I won't have a lot of explanation about the reasons why. It's easier to downvote than to analyze a text that appears incomprehensible.

I had a long time trying to understand the Anon's post before I finally realized that it was speciously written and not very much authoritative. Maybe it can be interpreted as approximately right for someone who knows about the inheritance; but I find it confusioning when read by someone having unsteady notions about inheritance, a subject not as clear as rock water in general – eyquem Aug 14 at 21:50.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions