Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Lost in the inheritance tree...

Reply
Thread Tools

Lost in the inheritance tree...

 
 
Adam Munoz Lopez
Guest
Posts: n/a
 
      06-06-2005
Can anyone help with this code... I have infinite
recursion but since I'm pretty new to Python (and
programming in general) I can't find where I did the
mistake.

Thanks a lot in advance.

Adam

*************************************************
import Tkinter
class RootFrame(Tkinter.Frame):
def
__init__(self,parent=None,myHeight=600,myWidth=800 ,myBd=3,\
myRelief=Tkinter.RIDGE):

Tkinter.Frame.__init__\

(self,parent,height=myHeight,width=myWidth,bd=myBd ,\
relief=myRelief)

self.grid()
self.grid_propagate(0)

self.createFrames()
self.showMsg()

def showMsg(self):
msg=Tkinter.Label(textFrame,text="Are you
thinking of an animal?")
msg.grid()

def createFrames(self):
textFrame=TextFrame(self,300,600)
textFrame.grid()
textFrame.grid_propagate(0)

def createButtons(self):
yesButton=Tkinter.Button(self,text="Yes")
yesButton.grid(row=1,sticky=Tkinter.E)
noButton=Tkinter.Button(self,text="No")
noButton.grid(row=1,sticky=Tkinter.W)

class TextFrame(RootFrame):
def __init__(self,parent,myHeight,myWidth):

RootFrame.__init__(self,parent,myHeight,myWidth)


rootFrame=RootFrame()
rootFrame.mainloop()






__________________________________________________ _________
Yahoo! Messenger - NEW crystal clear PC to PC calling worldwide with voicemail http://uk.messenger.yahoo.com
 
Reply With Quote
 
 
 
 
Rocco Moretti
Guest
Posts: n/a
 
      06-06-2005
Adam Munoz Lopez wrote:
> Can anyone help with this code... I have infinite
> recursion but since I'm pretty new to Python (and
> programming in general) I can't find where I did the
> mistake.


It really does help to start removing things trying to get the minimal
code which causes the problem.

(untested snippage follows)
> *************************************************
> import Tkinter
> class RootFrame(Tkinter.Frame):
> def __init__(self,parent=None,myHeight=600,myWidth=800 ,myBd=3,\
> myRelief=Tkinter.RIDGE):
> self.createFrames()
>
> def createFrames(self):
> textFrame=TextFrame(self,300,600)
>
>
> class TextFrame(RootFrame):
> def __init__(self,parent,myHeight,myWidth):
> RootFrame.__init__(self,parent,myHeight,myWidth)
>
> rootFrame=RootFrame()


It appears you create a RootFrame, which creates a TextFrame, which
initilizes as a RootFrame, thus creating a TextFrame which initilizes as
a RootFrame, thus creating a TextFrame which....
 
Reply With Quote
 
 
 
 
Jordan Rastrick
Guest
Posts: n/a
 
      06-06-2005
Although you get infinite recursion with this code, you still get
enough information on the error from the interpreter to help you debug.

Running IDLE, I get a traceback of:

File "C:/Documents and Settings/Jordan/Desktop/more_blah.py", line 11,
in __init__
self.createFrames()
File "C:/Documents and Settings/Jordan/Desktop/more_blah.py", line
19, in createFrames
textFrame=TextFrame(self,300,600)
File "C:/Documents and Settings/Jordan/Desktop/more_blah.py", line
31, in __init__
RootFrame.__init__(self,parent,myHeight,myWidth)

repeated indefinitely. At a glance, this tells you:

* That __init__ (of the RootFrame method) calls self.createFrames()
* createFrames(), in turn, calls TextFrame(self,300,600)
* This leads to RootFrame.__init__ being called once more

So theres youre infinite recursion. RootFrame's __init__ calls
createFrames which creates a new TextFrame - meaning TextFrame.__init__
gets called, and this calls its parent's __init__ method, and so on ad
infinitum.

This is a pretty good demonstration of the prinicple that you should do
as little as is nessecary to create an object - if possible, try to
calling other methods on an object in its __init__ method.

Without knowing more about youre program, and with only limited GUI
building experience (and none in Python), I'd guess the most likely
solution is for TextFrame to inherit from Tkinter.Frame directly -
having it inheret from RootFrame doesn't really make much sense as far
as I can see. TextFrame is, I would guess, intended as a component of
RootFrame, not a subclass. Thats another important OO prinicple - don't
overuse inheritance, often simple composition (one object having
another as an attribute) is the right solution.

If TextFrame really is supposed to inherit from RootFrame, try to
explain why.

 
Reply With Quote
 
Jordan Rastrick
Guest
Posts: n/a
 
      06-06-2005
comp.lang.python is a great newsgroup in that respect - so long as you
ask a semi-intelligent question, you nearly always end up with a quick
and helpful response.

Good luck with learning programming, and Python (IMO its one of the
best possible languages to do it in)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Struct inheritance against class inheritance johnsonlau C++ 1 07-21-2008 04:58 PM
Interface inheritance vs Implementation inheritance. Daniel Pitts Java 27 02-27-2008 01:37 AM
Private Inheritance and Publice Inheritance karthikbalaguru C++ 9 09-10-2007 01:05 PM
mul. inheritance & overloading operator new/delete solved by virtual base inheritance? cppsks C++ 0 10-27-2004 07:49 PM
Private access modifier and Inheritance (Inheritance implementation in Java) maxw_cc Java 1 12-21-2003 11:38 AM



Advertisments