Builtn super() function. How to use it with multiple inheritance? And why should I use it at all?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Jul 30 07:49:01 EDT 2010


On Fri, 30 Jul 2010 19:35:52 +1200, Gregory Ewing wrote:

> Steven D'Aprano wrote:
> 
>> super() is just as explicit as len(), or str.upper(). It says,
>> explicitly, that it will call the method belonging to one or more
>> superclass of the given class.
> 
> That's not strictly true. It will call a method belonging to some class
> in the mro of self, but that class is not necessarily in the base list
> of the class mentioned in the super() call. 

Yes, that's what I said. super() can visit any superclass of the given 
class, not just one of the immediate base class(es). That's why it's 
called super() rather than base() or parent(). It would be rather 
pointless if super() was limited to just the base classes.


> It's possible for a super()
> call to go "sideways" in the inheritance graph.

I doubt that very much. A class F can't inherit behaviour from a class E 
merely by virtue of them both being subclasses of the same hierarchy. If 
it did, that would be... disturbing.

Example:

E inherits from D.
D inherits from C and B.
C and B both inherit from A.
F also inherits from C.

F and E are "sideways" to each other (sibling classes?), but they don't 
inherit from each other.

Perhaps you're referring to the angled lines in a diagram such as:

      A
     / \
    C   B
     \ /
      D
     / \
    E   F

F and E don't inherit from each other, because they are sidewards to each 
other (they are not in each other's MRO). Regardless of the angles we 
draw the lines, all of D, C, B, A are above E (and F). Or downwards if 
you prefer to reverse the diagram. Yes, a super call might jog left from 
C to B, but only when being called from one of the lower classes D-F. 
That's still an upwards call relative to the originator, not sidewards.



-- 
Steven



More information about the Python-list mailing list