By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,172 Members | 727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,172 IT Pros & Developers. It's quick & easy.

Inheritance and recursion problem

P: n/a
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.
Nov 25 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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(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>

Nov 25 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.