grandparent method with super

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Thu Apr 5 19:41:45 EDT 2007


kyosohma at gmail.com a écrit :
> On Apr 5, 3:19 pm, Martin Manns <mma... at gmx.de> wrote:
> 
>>Hi,
>>
>>I have a class structure as follows and I would like to invoke the
>>method A.m() from D.m
>>
>>class A(object):
>>        def m(self):
>>class B(A):
>>        def m(self):
>>class C(A):
>>        def m(self):
>>class D(B,C):
>>        def m(self):
>>                # Call A.m with super?
>>
(snip)
> I'm not sure if this is what you want, but it's my best guess:
> 
> class A(object):
>         def m(self):
>             print "I'm the original"
> class B(A):
>         def m(self):
>             print 'B class here'
> class C(A):
>         def m(self):
>             print 'C class here'
> class D(B,C):
>         def m(self):
>                 x = A()
>                 x.m()

Err... This will call A.m(x), not A.m(self). The correct thing to is:
class D(B,C):
     def m(self):
         A.m(self)

But this doesn't solve the OP's problem, which is to not hard-code the 
concrete base class to call (hence the question about how to do this 
using super)



More information about the Python-list mailing list