Q? Calling nearest inherited method

Guido van Rossum guido at python.org
Wed May 17 14:37:00 EDT 2000


Laurent POINTAL wrote:
> 
> On Wed, 17 May 2000 11:54:53 +0000 (GMT), Oleg Broytmann
> <phd at phd.russ.ru> wrote:
> 
> >: On Wed, 17 May 2000, Laurent POINTAL wrote:
> >: > Given a class hierarchy like this:
> >: > A
> >: > B inherits A
> >: > C inherits B
> >: >
> >: > A define dothis method.
> >: > C define dothis method.
> >: >
> >: > In the C.dothis, I wants to call my nearest parent class dothis
> >: > method. That is, call B.dothis if it is defined, or A.dothis if it is
> >: > defined...
> >:
> >:    I think B.dothis() will do it just right, no?
> 
> I was afraid it resolve in a single (B) method search... but it search
> also in B parents (even when prefixed explicitely with B).
> 
> Thanks.
> 
> Python is really wonderful.

There's a better way.  (I believe Jeremy Hylton first suggested this; he saw
it as an idiom in another language:)

class C(B):
    super_doit = B.doit
    def doit(self, arg):
        self.super_doit(arg)

This avoids the need to explicitly pass self.

-- 
--Guido van Rossum (home page: www.python.org/~guido/)



More information about the Python-list mailing list