super() and automatic method combination
Scott David Daniels
Scott.Daniels at Acm.Org
Wed May 18 11:14:58 EDT 2005
Laszlo Zsolt Nagy wrote:
>
>> The trick is that C.f only calls A.f, but A.f needs to end up calling
>> B.f when it is used in a C.
>>
>>
> I believe your response only applies to single inheritance. For classes
> with muliple bases classes, you need to call the base methods one by one.
>
> BTW I prefer to call the base methods in this form:
>
> class AB(A,B):
> def f(self):
> A.f(self)
> B.f(self)
>
> This arises the question: is there a difference between these:
>
> super(A,self).f() # I do not use to use this....
> A.f(self)
>
The difference is when you have a diamond inheritance diagram.
Here is a simple example:
class Bottom(object):
def f(self):
print 'Bottom'
class A(Bottom):
def f(self):
print 'A',
super(A, self).f()
class B(Bottom):
def f(self):
print 'B',
super(B, self).f()
class C(A, B):
def f(self):
print 'C',
super(C, self).f()
C().f()
C A B Bottom
-------
Versus:
-------
class Bottom(object):
def f(self):
print 'Bottom'
class A(Bottom):
def f(self):
print 'A',
Bottom.f(self)
class B(Bottom):
def f(self):
print 'B',
Bottom.f(self)
class C(A, B):
def f(self):
print 'C',
A.f(self)
B.f(self)
C().f()
C A Bottom
B Bottom
--Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list