[Python-Dev] Things to Know About Super

Joel Bender jjb5 at cornell.edu
Thu Aug 28 20:36:00 CEST 2008


> Do you have a real-life example of this where multiple
> inheritance is actually used?

I have built a framework that I have called the "capability pattern" 
which uses multiple inheritance in a way that might be unique (I'm not 
familiar enough with other frameworks to know for sure).

There are two classes, a Collector and a Capability.  The result of 
calling a function of the collector is a list of results of calling the 
functions of the bound capabilities.  For example, these three are 
capability classes:

     class X(Capability):
         def f(self): return 'X.f'

     class Y(Capability):
         def g(self): return 'Y.g'

     class Z(Capability):
         def f(self): return 'Z.f'
         def g(self): return 'Z.g'

Now to create a sample collector:

     class A(Collector, X, Y, Z): pass

Calling A().f() returns ['X.f', 'Z.f'].

I use this pattern in a web application.  The do_GET call is mapped into 
do_SHOW, and each 'capability' can return something from its do_SHOW 
(usually a <div> element) and the results are sent back to the user.  In 
my case I have lots of combinations of capabilities that can be mixed 

I decided to use multiple inheritance over other patterns because I 
wanted to leverage isinstance(obj,Y) to indicate that some object has 
some capability, and not having to duplicate the method resolution order 
code for other kinds of methods is really nice.

> A non-contrived example or two would be a good thing to
> have in tutorials etc. where super() is discussed. It
> would help to convey the kinds of situations in which
> use of super() is and is not appropriate.

So this is a collection of cooperative classes, and super() isn't used.


More information about the Python-Dev mailing list