Parent references: hot or not?

Alex Martelli aleax at aleax.it
Sat Apr 19 13:35:45 EDT 2003


Dylan Reinhardt wrote:

> On Sat, 2003-04-19 at 10:00, Dylan Reinhardt wrote:
> 
>> Sounds like I should be sure to write a __del__ method for Container
>> that ensures things work correctly.
> 
> And upon re-reading your post you say *not* to do that... :-)
> 
> I'd like to understand that a bit better.  What would be wrong with:
> 
> class Container:
> 
>    def __del__(self):
>       # ensure all Items are deleted first
>       for item in self.items.keys():
>          del self.items[item]

It would never be executed when self.items is non-empty: since
each item in self.items holds a reference to self, self can
never be destroyed in the normal way (by the number of references
on it going to zero), *AND*, the very existence of a __del__
STOPS the garbage collector from collecting reference-loops.


Alex





More information about the Python-list mailing list