On 29/03/17 15:33, Rafael Knuth wrote: > I am trying to wrap my head around the super constructor.
This is one of these cases where it matters whether you are using Python v2 or v3. Use of super in v3 is much easier. It looks from your examples like you are using v2 but it would be good to confirm that. > class A: > def __init__(self): > print("world") > > class B(A): > def __init__(self): > print("hello") > super(B, self).__init__() > > B() > > Then I changed the parent class A like this, as I wanted to test how > the code would look like if I passed arguments: > > class A: > def __init__(self, message): > self.message = message > print(message) > > I then modified the child class B like this: > > class B(A): > def __init__(self, message): > print("This is the message from your parent class A:") > super(B, self).__init__(message) > > B("BlaBla") > > That works, however I am not sure about what exactly happens inside the code. Yes, you have the mechanism right. As to what exactly happens inside the interpreter I'll leave that for those who care about such things :-) > What I am concerned about is whether the argument is being actually > inherited from the parent class A or does B overwrite the argument. The argument is just a parameter of the init() method like any other argument. It is effectively a local variable. The self.message attribute however is instantiated in A and inherited by B. (At least conceptually. I'll let the interpreter gurus answer how it work in the implementation) Thus in B you could access self.message directly - and in a normal method that might be the right thing to do. But in a constructor/initializer you should leave the initializing to super() -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor