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 08:04:21 EDT 2010


On Fri, 30 Jul 2010 19:37:29 +1200, Gregory Ewing wrote:

> Steven D'Aprano wrote:
>> On Thu, 29 Jul 2010 19:29:24 +0200, Jean-Michel Pichavant wrote:
>  >
>>>"mro" would have been the proper name for "super".
>> 
>> That's your opinion. In any case, whether super() was called super() or
>> mro() or aardvark() makes no difference to the functionality or whether
>> it is useful.
> 
> I think the point is that the name is misleading, because it makes it
> *sound* like it's going to call a method in a superclass, when it fact
> it might not.

I'm not sure I understand your point here. If you call super() from a 
method that doesn't exist in any superclass, then you are correct, it 
won't call a method in a superclass, and will raise AttributeError.

But in the more sensible case that you only call super() when there is 
actually something to inherit, then of course it calls the method in a 
superclass. It certainly doesn't call methods from arbitrary unrelated 
classes, only those which are in the MRO. That is, superclasses.

If Z is below A in the hierarchy, then we have no difficulty in 
identifying Z as a subclass of A, and likewise we should have no problem 
with identifying A as *a* (and not "the") superclass of Z, no matter how 
distant they are or how tangled the DIG between them. 

The exception would be if you could have loops in the class hierarchy, in 
which case the concepts of super- and sub-classes breaks down completely. 
But even if some other languages allowed that, Python doesn't, so we're 
safe.



-- 
Steven



More information about the Python-list mailing list