How to delete yourself????

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • christian.siegl@gmail.com

    #1

    How to delete yourself????

    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(sel f, child):
    # delete child from the list.... the way this is done is not
    our
    # problem here


    class child:

    MyParent = 0

    def deleteMe(self):
    MyParent.Remove Child(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

  • Kay Schluehr

    #2
    Re: How to delete yourself????


    christian.siegl @gmail.com 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(sel f, child):
    > # delete child from the list.... the way this is done is not
    > our
    > # problem here
    >
    >
    > class child:
    >
    > MyParent = 0
    >
    > def deleteMe(self):
    > MyParent.Remove Child(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.re move(child) if the
    caller holds a child object but not the parent? Otherwise it should be
    sufficient to call parent.remove(c hild).

    Kay

    Comment

    • christian.siegl@gmail.com

      #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

      Comment

      • Istvan Albert

        #4
        Re: How to delete yourself????

        > But is this nice code??? Is there another way to delete yourself???

        I think you are overcomplicatin g 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.

        Comment

        • christian.siegl@gmail.com

          #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

          Comment

          Working...