Lost in the inheritance tree...

Jordan Rastrick jrastrick at student.usyd.edu.au
Mon Jun 6 17:53:34 CEST 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.




More information about the Python-list mailing list