inheritance problem

Miki Tebeka miki.tebeka at zoran.com
Thu Feb 19 08:28:30 CET 2004


Hell Den,

> Example:
> ------------
> class c1:
>   def method1(self):
>     print 'class c1, method1'
>   def method2(self):
>     print 'class c1, method2'
>     self.method1()
> 
> class c2(c1):
>   def method1(self):
>     print 'class c2, method1'
>     c1.method2(self)
>   def method2(self):
>     print 'class c2, method2'
> 
> c = c2()
> c.method1()
> ------------
> i expect :
> class c2, method1
> class c1, method2
> class c1, method1
> 
> but i get this:
> class c2, method1
> class c1, method2
> class c2, method1
> class c1, method2
> ...
> RuntimeError: maximum recursion depth exceeded
> 
> How fix this?
You are thinking C++ (I guess), all methods in Python are "virtual".
What happens is:
c.method1 invokes c2.method1 which calls c2.method2 on self.
c2.method2 calls self.method1 *which is c2.method1*, hence the
recursion.

If you can specify the motivation behind this desing mabye we can help
you overcode it.

HTH.
Miki



More information about the Python-list mailing list