trouble understanding super()

John Salerno johnjsal at NOSPAMgmail.com
Mon Jul 31 13:43:35 EDT 2006


Duncan Booth wrote:
> John Salerno wrote:
> 
>> But after super(D, self).met() is called, doesn't that then call both 
>> super(B, self).met() and super(C, self).met()? If so, how does that 
>> avoid calling A.met twice? Or is that not what's happening?
> 
> If you have an instance of a B then super(B,self).met() will call A.met(), 
> but if self is actually an instance of a D, then super(B,self).met() 
> actually calls C.met().
> 
> That is why super needs both the class and the instance: so it can jump 
> sideways across the inheritance diamond instead of always passing calls to 
> the base of the current class.

Oh, I think I get it! So what's happening is this:

1. the MRO in this case is always (D, B, C, A, object)
2. super(D, self).met() looks in B
3. super(B, self).met() looks in C
4. super(C, self).met() looks in A

Right? So it's like making a ladder? I guess my confusion came when I 
thought that a call to super(B, self).met() also called A.met, but I 
guess it stops at C and lets C call it.



More information about the Python-list mailing list