to ask an container instance to suicide

Virgil Dupras hardcoded.software at gmail.com
Mon Nov 20 07:15:14 EST 2006


jm.suresh at no.spam.gmail.com wrote:
> Hi,
>  I have containers to hold geometrical objects. The members in these
> containers can again be containers them self. Now, In the program I
> would be moving objects from one container to another. In this process,
> If the number of members in one of the containers becomes just one, I
> want to delete that container and have only its only member in place of
> it in the parent container.
>  I have implemented setting and getting the container members using
> property().
>
> Right now, I am planning to do this this way:
>  Maintain a reference to parent container in each container.
>  When the condition is detected, do self.parent.deleteObject(self) .
>
> def deleteObject(self,memberId):
>    index = self._members.index(memberId)
>    self._members[index] = memberId._members[0]
>    del memberId
>
> Will this work. Or is there any other simple way to do this?
> --
> Suresh

I don't understand what you want to achieve, but there are 2 things I
can say: "del memberId" is useless, it is deleted anyway when memberId
goes out of scope. Because you have a 2-way reference, have one of them
(the parent reference would be better) a weakref. Else, you'll be
leaking memory.




More information about the Python-list mailing list