inheritance problem

D

Den Ivanov

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?

PS. python 2.1.3
 
M

Miki Tebeka

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
 
J

Joe Mason

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

C++ and C# are the only languages I know of that are broken this way.

(Because it means their classes are not truly OO by default, that's why.
It causes confusion just like this. I see the benefits of being able to
skip the virtual table lookup for speed, but that should be the
exception that needs to be turned on with a keyword, not the standard
behaviour.)
but i get this:
class c2, method1
class c1, method2
class c2, method1
class c1, method2
...
RuntimeError: maximum recursion depth exceeded

How fix this?

class c1:
def base_method1(self):
print 'class c1, method1'
def method1(self):
self.base_method1()
def method2(self):
print 'class c1, method2'
self.base_method1()

Or

class c0:
def method1(self):
print 'class c1, method1'

class c1(c0):
def method2(self):
print 'class c1, method2'
c0.method1(self)

Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,966
Members
47,516
Latest member
ChrisHibbs

Latest Threads

Top