[Tutor] What is super for?

Alan Gauld alan.gauld at btinternet.com
Tue Jun 29 10:22:44 CEST 2010


"Steven D'Aprano" <steve at pearwood.info> wrote

> Even if C and D don't share the same parent class, they might 
> inherit
> from a chain of classes, any of which defines f. Working out which
> method f to call can be complicated.
>
> super() handles all that complication for you. You just write:

Yes, I do use super for multiple inheritance.
But I don't use MI all that often in Python!

> them, so you don't have to, and my summary is:
>
> * Don't use multiple inheritance.

This depends on the language. I started my OO programming
using Lisp Flavours in the 1980's and it was all about MI. You
created classes by mixing in different abstract classes (Mixins).
Every object used MI extensively. But Flavors was designed
for that and had many control mechanisms to fine tune the
MRO. When I first moved to C++ I took my Flavors style with
me and tried to use it - it was a nightmare because C++ had
a fixed MRO that resulted in multiple calls to methods etc.
I very quickly changed my OO Design style!

Python is more like C++ than Flavors so in this context Steven's
advise is sound, only use MI if its really needed. And to some
extent thats true of single inheritance too - it should be to
support an "is-a" relationship, not just an implementation
shortcut.

> And here are some links from Michele Simionato on why mixins and
> multiple inheritance should be avoided unless you really, really 
> need
> it, and how to avoid needing it:

Again, I add the caveat that if the language supports it, and the 
paradigm
is common its not that bad. But most OO languages don't support it 
very well!
Also MI from concrete classes rather than abstracts makes the problems
much, much worse. A mixin class should be abstract.

And finally I'll say I'd much rather have the option of MI than not.
In those cases where you really need it is vastly preferable to
mass delegation - easier to write and easier to maintain.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list