sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
christian.siegl@gmail.com's Avatar

How to delete yourself????


Question posted by: christian.siegl@gmail.com (Guest) on October 14th, 2005 03:35 PM
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

4 Answers Posted
Kay Schluehr's Avatar
Guest - n/a Posts
#2: Re: How to delete yourself????


Join Bytes! wrote:[color=blue]
> 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)
> ################################################## ############[/color]

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

christian.siegl@gmail.com's Avatar
christian.siegl@gmail.com October 14th, 2005 04:05 PM
Guest - n/a Posts
#3: Re: How to delete yourself????

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

Istvan Albert's Avatar
Guest - n/a Posts
#4: Re: How to delete yourself????

> 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.

christian.siegl@gmail.com's Avatar
christian.siegl@gmail.com October 17th, 2005 05:55 PM
Guest - n/a Posts
#5: Re: How to delete yourself????

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

 
Not the answer you were looking for? Post your question . . .
196,799 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 196,799 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors