[Tutor] quirky multiple inheritance example!?
Kent Johnson
kent37 at tds.net
Thu Feb 9 12:01:54 CET 2006
Alan Gauld wrote:
>>> The problem is that super appears to only work with single inheritance.
>
>
>> That's ironic - super() is intended to ease some of the problems with
>> multiple inheritance especially diamond patterns. Some docs are here:
>> http://www.python.org/2.2.3/descrintro.html#cooperation
>>
>> The problem with the OP is that not all the classes involved use super().
>
> I tried that. Before posting I went back and reread the super docs and
> tried building an MI lattice but, whatever I did, super only ever called
> the
> first listed superclass. If it had a super it called its superclass but the
> second superclasss never got initialised so far as I could tell!
Works for me. Each class must call super(...).__init__(). The first arg
to super() must be the class in which the call appears. One mistake is
to call super(self.__class__, self) - this is not correct. Here is an
example:
>>> class A(object):
... def __init__(self):
... print 'A.__init__()'
... super(A, self).__init__()
...
>>> class B(object):
... def __init__(self):
... print 'B.__init__()'
... super(B, self).__init__()
...
>>> class C(A, B):
... def __init__(self):
... print 'C.__init__()'
... super(C, self).__init__()
...
>>> C()
C.__init__()
A.__init__()
B.__init__()
<__main__.C object at 0x00A32BB0>
Kent
More information about the Tutor
mailing list