Inheritance and recursion problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Spiro

    #1

    Inheritance and recursion problem

    Hi all,
    i'm trying to make some user interface objects in python.

    I have classes XWindow and XMainWindow(XWi ndow) (XMainWindow inherited
    from XWindow)

    XWindow have all drawing functionality and Windows[] object which holds
    all child windows.
    Function Draw looks like this:
    def Draw(self):
    self.Back.blit
    for wnd in self.Windows:
    wnd.Draw(Offset X, OffsetY)

    Now in main module i make objects:
    RootWindow=XWin dow()
    MW=XMainWindow( )
    RootWindow.Widn ows.append(MW)

    Now the problem:
    When i call RootWindow.Draw () he calls wnd.Draw where wnd is XMainWindow
    from RootWindow's Windows[] collection.
    Now, we are in MW's Draw and MW's Windows[] collection should be empty
    but somehow he has itself (well, new instance of XMainWindow) in this
    collection and i got unlimited reference.

    ....
    While writing this post i tried something:
    instead of using RootWindow.Wind ows.append(MW)
    i used RootWindow.Wind ows=[MW] and now it's OK.

    I'm happy now, my code works, but i don't know what was happening there:
    why append made new instance of object instead of passing existing object.
  • Fredrik Lundh

    #2
    Re: Inheritance and recursion problem

    Spiro wrote:
    XWindow have all drawing functionality and Windows[] object which holds
    all child windows.
    >
    Function Draw looks like this:
    def Draw(self):
    self.Back.blit
    for wnd in self.Windows:
    wnd.Draw(Offset X, OffsetY)
    >
    Now in main module i make objects:
    RootWindow=XWin dow()
    MW=XMainWindow( )
    RootWindow.Widn ows.append(MW)
    just a guess: you've written

    class XWindow:
    Windows = [] # class attribute, shared by all instances

    instead of

    class XWindow:
    def __init__(self, ...):
    self.Windows = [] # create new instance attribute

    </F>

    Comment

    Working...