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

Antoon Pardon antoon.pardon at vub.be
Tue Mar 27 06:32:02 EDT 2018


On 27-03-18 08:21, Gregory Ewing wrote:
> 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.

This doesn't follow. The idea in the first paragraph
expresses that if you need to call an inherrited method
you must do it by using super. It doesn't express that
you always need to call inherrited methods. 

>
> 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.

I don't understand your point here. If your method
doesn't call the inherited method, it doesn't matter that
it doesn't do so by not using super or by not using
the parent class explicitly.

> Or you want to
> do something additional, but the base method must be
> called *first*.

You mean actually first or just before excuting the rest of
the code in this method?
>
> 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.

I doubt that unless you don't mind multiple calls
to the same inherrited methods now and then.

>
> Yes, diamonds can be a problem. You can mitigate that
> by (1) avoiding diamonds; 

In that case we just don't need inherritance. We
can solve it all by using composition instead of
inheritance.

> (2) if you can't avoid
> diamonds, avoid putting methods in the apex class that
> get called as inherited methods;

That means we can't use any standard library class as the
apex class.





More information about the Python-list mailing list