Inheritance and recursion problem

S

Spiro

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

I have classes XWindow and XMainWindow(XWindow) (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(OffsetX, OffsetY)

Now in main module i make objects:
RootWindow=XWindow()
MW=XMainWindow()
RootWindow.Widnows.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.Windows.append(MW)
i used RootWindow.Windows=[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.
 
F

Fredrik Lundh

Spiro said:
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(OffsetX, OffsetY)

Now in main module i make objects:
RootWindow=XWindow()
MW=XMainWindow()
RootWindow.Widnows.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>
 

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

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,200
Latest member
LaraHunley

Latest Threads

Top