To super or not to super (Re: Accessing parent objects)

Gregory Ewing greg.ewing at canterbury.ac.nz
Tue Mar 27 02:21:38 EDT 2018


The idea that super() is *always* the right way to call
inherited methods in a multiple inheritance environment
seems to have been raised by some people to the level
of religous dogma.

I don't buy it. In order for it to work, the following
two conditions must hold:

1) All the methods involved have "additive" behaviour,
i.e. the correct thing to do is always to call *all*
versions of the method with the same arguments.

2) It doesn't matter what order they're called in.

The trouble is, those conditions don't always hold.
Often when overriding a method, you want to do something
*instead* of what the base method does. Or you want to
do something additional, but the base method must be
called *first*.

In those situations, there's no way to guarantee that
the right thing will happen by using super().

On the other hand, explicit inherited method calls give
you precise control over what happens and what order it
happens in, with no chance of it being messed up by
someone ineriting from you.

Yes, diamonds can be a problem. You can mitigate that
by (1) avoiding diamonds; (2) if you can't avoid
diamonds, avoid putting methods in the apex class that
get called as inherited methods; (3) if you can't avoid
either of those, as far as I can tell the situation is
intractable and you need to rethink your design.

-- 
Greg



More information about the Python-list mailing list