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

How to delete yourself????

P: n/a
Hello,
I got a problem deleting objects, which are placed in a hirarchy....

Asume we have the following code:
################################################## ######
class parent:

MyChilds = [] # this list is filled with childs....

def AddChild(self, child):
# add childs here, however this is done, it's not the point...
child.MyParent = self

def RemoveChild(self, child):
# delete child from the list.... the way this is done is not
our
# problem here
class child:

MyParent = 0

def deleteMe(self):
MyParent.RemoveChild(self)
################################################## ############

So my code looks something like that.... and it works. I works because
you do not touch any member variables in class child after deleting
it... But is this nice code??? Is there another way to delete
yourself???

Thanks for any comments...
Chris

Oct 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

ch*************@gmail.com wrote:
Hello,
I got a problem deleting objects, which are placed in a hirarchy....

Asume we have the following code:
################################################## ######
class parent:

MyChilds = [] # this list is filled with childs....

def AddChild(self, child):
# add childs here, however this is done, it's not the point...
child.MyParent = self

def RemoveChild(self, child):
# delete child from the list.... the way this is done is not
our
# problem here
class child:

MyParent = 0

def deleteMe(self):
MyParent.RemoveChild(self)
################################################## ############


Please help me understand the child class. It does not make much sense
to me at all.

Why is it not sufficient to call child.parent.remove(child) if the
caller holds a child object but not the parent? Otherwise it should be
sufficient to call parent.remove(child).

Kay

Oct 14 '05 #2

P: n/a
well, of course these classes are dummies..... in my application they
have of course more functionality.... they shall only describe the
problem here....

one certain event which could appear is, that the child wants to delete
itself by calling self.deleteMe() somewhere in one of the member
functions (which are not listed here)

i mean it is not the point, that this does not work (it works indeed)
but is this nice code and are there different ways to do this???

chris

Oct 14 '05 #3

P: n/a
> But is this nice code??? Is there another way to delete yourself???

I think you are overcomplicating things.

Don't think of it as deleting 'itself', since what you seem to need is
deleting a reference to the instance.

In your example the parent is a container and it usually makes more
sense to have the only the container deal with adding and deleting
elements, that way your objects are less coupled.

Istvan.

Oct 14 '05 #4

P: n/a
Well, this might be the best way, but my special case is programming a
GUI. And a (child) window receives a close event and wants to destroy
itself. The main window (parent) will never notice that (or its lots of
code and not very nice....). So that is the case and there is no way
that the parent deletes the child.

By the way, I am aware of the fact that I'm just deleting a reference.
But on deleting the last reference (and that's what I wanna do here)
the object will be destroyed........

chris

Oct 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.