'super' to only be used for diamond inheritance problems?

Alex Martelli aleax at mail.comcast.net
Mon Oct 31 10:07:26 EST 2005


Alex Hunsley <lard at tardis.ed.ac.molar.uk> wrote:

> I've seen a few discussion about the use of 'super' in Python, including
> the opinion that 'super' should only be used to solve inheritance 
> diamond problem. (And that a constructor that wants to call the 
> superclass methods should just call them by name and forget about super.)
> What is people's opinion on this? Does it make any sense?

No, it makes no sense whatsoever.  Any situation where multiple
inheritance is possible, diamonds or no diamonds, can benefit from
cooperative superclass calling where needed (e.g., __init__).  Consider:

class A(B, C): ...

should A know or care at all whether B and C share common bases?  Of
course not -- why should A have to be burdened with that knowledge?!  To
ensure __init__ is called on each base once and only once,
systematically using super(A, self) is the best and simplest way.

Another way to put it: there's ALWAYS "meshes" (diamonds or more
complicated forms), whenever multiple inheritance occurs, because (in
the newstyle object model, and the other one only exists for legacy
reasons and shouldn't be used in new code) object is always among the
ancestors of EVERY class.  So, again: use super.


Alex



More information about the Python-list mailing list