Q? Calling nearest inherited method

Laurent POINTAL pointal at lure.u-psud.fr
Thu May 18 11:01:54 CEST 2000


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.

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 



More information about the Python-list mailing list