Q? Calling nearest inherited method
Fred Gansevles
gansevle at cs.utwente.nl
Thu May 18 07:02:38 EDT 2000
In article <3923af51.3872163586 at news.u-psud.fr>,
pointal at lure.u-psud.fr wrote:
> On Wed, 17 May 2000 18:37:00 GMT, Guido van Rossum <guido at python.org>
> wrote:
>
> >: 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.
>
> Not sure its really better...
> 1) The lowest problem: you have to define it for each function in each
> class in the hierarchy to have a clean model.
> 2) The biggest problem, it can go into infinite recursion...
>
> Here is an example of the biggest problem:
>
> >>> class A :
> ... def dothis(self) :
> ... print "In A.dothis."
> ...
> >>> class B(A) :
> ... super_dothis = A.dothis
> ...
> >>> class C(B) :
> ... super_dothis = B.dothis
> ... def dothis(self) :
> ... print "In C.dothis."
> ... self.super_dothis()
> ...
> >>> c=C()
> >>> c.dothis()
> In C.dothis.
> In A.dothis.
>
> [Until here, its well. Now, introduce a new D subclass, and as we have
> done for B, implement the super_dothis...]
>
> >>> class D(C) :
> ... super_dothis = C.dothis
> ... def hello(self) :
> ... self.dothis()
> ...
> >>> d=D()
> >>> d.hello()
> In C.dothis.
> In C.dothis.
> In C.dothis.
> In C.dothis.
> In C.dothis.
> In C.dothis.
> In C.dothis.
> ...
> [here hitting ctrl-C]
>
> So I will still with the <parentClass>.<method>(self...) usage which
> works.
An intermediate approach could be:
def super(self):
return self.__class__.__bases__[0]
class D(C):
def hello(self):
super(self).dothis(self)
>
> Thanks.
>
> A+
>
> Laurent.
>
> ---
> Laurent POINTAL - CNRS/LURE - Service Informatique Experiences
> Tel/fax: 01 64 46 82 80 / 01 64 46 41 48
> email : pointal at lure.u-psud.fr ou lpointal at planete.net
>
--
-----------------------------------------------------------------------
---- Linux/Windows-NT/IntraNetware Administrator ----
-- Whenever it sounds simple, I've probably missed something! ... Me --
-----------------------------------------------------------------------
Sent via Deja.com http://www.deja.com/
Before you buy.
More information about the Python-list
mailing list