multiple inheritance super()
rafi
rafi at free.fr
Tue Jul 26 17:27:25 EDT 2005
Peter Hansen wrote:
> km wrote:
>
>> Hi all,
>>
>> In the following code why am i not able to access class A's object
>> attribute - 'a' ? I wishto extent class D with all the attributes of
>> its base classes. how do i do that ?
[snip]
> Each class should do a similar super() call, with the appropriate name
> substitutions.
[snip]
> -Peter
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'
def foo (self):
print 'A.foo'
class B (object):
def __init__ (self):
super (B, self) .__init__ ()
print 'i am a B'
def foo (self):
print 'B.foo'
class C (A, B):
def __init__ (self):
super (C, self) .__init__ ()
print 'i am a C'
c = C ()
c.foo ()
#--8<---
aerts $ python2.4 inheritance.py
i am a B
i am an A
i am a C
A.foo
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):
i am an A
i am a B
i am a C
A.foo
Thanks
--
rafi
"Imagination is more important than knowledge."
(Albert Einstein)
More information about the Python-list
mailing list