[Tutor] Why super does not work !
Steven D'Aprano
steve at pearwood.info
Tue Jan 18 00:09:35 CET 2011
Karim wrote:
>
>
> Hello,
>
> I implemented Observer DP on a listbox (Tkinter) as follows and I don't
> understand why super() is not working and Observable.__init__(self) is
> working, cf below:
You seem to be confused about your class design. On the one hand, you
inherit from Listbox, but then you *also* use composition and/or
delegation on a Listbox instance:
class ListObservable(Listbox, Observable):
def __init__(self):
[...]
self.liste = Listbox(listvariable=self.listeContenu,
selectmode='single')
That means that a ListObservable instance both *is* a Listbox and
*contains* a Listbox at the same time. I'm not saying this is
necessarily wrong, but it is unusual, and confuses the model. At the
very least, you need to document why you have done this.
> class ListObservable(Listbox, Observable):
> """Creation de widget Listbox"""
> def __init__(self):
> super(ListObservable, self).__init__()
> #Observable.__init__(self)
Are both Listbox and Observable documented as suitable for multiple
inheritance? My guess is that Listbox is not, and I can see from your
source code that Observable is *not* suitable for multiple inheritance.
If Listbox is documented as suitable for multiple inheritance, then it
is a bug in Listbox. If it is not, then it is a bug in your code, by
using it for multiple inheritance.
Multiple inheritance in Python is cooperative, not enforced, and it is
tricky to get right and is *very* sensitive to any class which fails to
cooperate. Thousands and thousands of words have been written on the
perils and difficulties of multiple inheritance, particularly by Michele
Simionato who I consider to be THE authority on MI in Python.
Some of the most important articles are:
Michele Simionato:
Things to Know About Python Super
http://www.artima.com/weblogs/viewpost.jsp?thread=236275
http://www.artima.com/weblogs/viewpost.jsp?thread=236278
http://www.artima.com/weblogs/viewpost.jsp?thread=237121
Mixins considered harmful:
http://www.artima.com/weblogs/viewpost.jsp?thread=246341
http://www.artima.com/weblogs/viewpost.jsp?thread=246483
http://www.artima.com/weblogs/viewpost.jsp?thread=254367
http://www.artima.com/weblogs/viewpost.jsp?thread=254507
Generic functions vs mixins:
http://www.artima.com/weblogs/viewpost.jsp?thread=237764
Straits:
http://www.artima.com/weblogs/viewpost.jsp?thread=246488
James Knight:
Python's Super Considered Harmful
http://fuhm.net/super-harmful/
Summary: this is *not* a bug in super. It's really a side-effect of the
fact that multiple inheritance itself is often the wrong thing to use,
and even when it is right, it is often tricky to get it right. To put it
another way: don't use multiple inheritance unless you have to, there
are better ways, such as by composition.
--
Steven
More information about the Tutor
mailing list