Lost in the inheritance tree...

Discussion in 'Python' started by Adam Munoz Lopez, Jun 6, 2005.

  1. 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
     
    Adam Munoz Lopez, Jun 6, 2005
    #1
    1. Advertising

  2. 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....
     
    Rocco Moretti, Jun 6, 2005
    #2
    1. Advertising

  3. 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.
     
    Jordan Rastrick, Jun 6, 2005
    #3
  4. Re: Lost in the inheritance tree... THANKS!

    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)
     
    Jordan Rastrick, Jun 6, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. B Young
    Replies:
    1
    Views:
    432
    B Young
    Nov 16, 2003
  2. Ramkumar Menon

    B+ Tree versus Ternary Search Tree

    Ramkumar Menon, Aug 16, 2005, in forum: Java
    Replies:
    2
    Views:
    1,620
    Roedy Green
    Aug 16, 2005
  3. Ramkumar Menon

    B+ Tree versus Ternary Search Tree

    Ramkumar Menon, Aug 16, 2005, in forum: Java
    Replies:
    0
    Views:
    449
    Ramkumar Menon
    Aug 16, 2005
  4. Ramkumar Menon

    B+ Tree versus Ternary Search Tree

    Ramkumar Menon, Aug 16, 2005, in forum: Java
    Replies:
    1
    Views:
    464
    Andrew Thompson
    Aug 16, 2005
  5. Stub

    B tree, B+ tree and B* tree

    Stub, Nov 12, 2003, in forum: C Programming
    Replies:
    3
    Views:
    10,183
Loading...

Share This Page