multiple inheritance super()
Scott David Daniels
Scott.Daniels at Acm.Org
Tue Jul 26 19:59:59 EDT 2005
rafi wrote:
> A related question is about the order of the __init__ calls. Considering
> the following sample:
>
> #--8<---
> class A (object):
> def __init__ (self):
> super (A, self) .__init__ ()
> print 'i am an A'
> class B (object):
> def __init__ (self):
> super (B, self) .__init__ ()
> print 'i am a B'
> class C (A, B):
> def __init__ (self):
> super (C, self) .__init__ ()
> print 'i am a C'
> c = C ()
>
> aerts $ python2.4 inheritance.py
> i am a B
> i am an A
> i am a C
>
> I do understand the lookup for foo: foo is provided by both classes A
> and B and I do not state which one I want to use, so it takes the first
> one in the list of inherited classes (order of the declaration). However
> I cannot find an explanation (I may have googled the wrong keywords) for
> the order of the __init__ calls from C. I was expecting (following the
> same order as the method lookup):
This should make it clear:
class A (object):
def __init__ (self):
print '<A>',
super (A, self) .__init__ ()
print '</A>'
class B (object):
def __init__ (self):
print '<B>',
super (B, self) .__init__ ()
print '</B>'
class C (A, B):
def __init__ (self):
print '<C>',
super (C, self) .__init__ ()
print '</C>'
C()
--Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list