On Wed, Mar 29, 2017 at 10:32:52PM +0100, Alan Gauld via Tutor wrote: > 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.
It can't be Python 2, because A doesn't inherit from object. In that case, B's attempt to call super() would fail. [...] > > 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 :-) The only real magic here is in super(). Everything else is just normal calling methods with arguments. What super() does is return a special object, let's call it S. When you call S.__init__, S is smart enough to ignore it's own __init__ method, and instead search B's inheritance chain (called the MRO, or Method Resolution Order), returning the first __init__ it finds. For B, the MRO is really short: first it looks at class A, then it looks at A's parent, namely `object`, which is the ultimate top of all inheritance chains in Python 3. But in general, the MRO might be very long, it might contain branches, and the same class might be included multiple times. By using super(), Python will ensure that each superclass method is called at most *one* time, in the right order, no matter how many superclasses are involved. -- Steve _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor