Destructor never called ???

Thomas Heller theller at python.net
Fri Sep 20 14:52:10 EDT 2002


Tim Peters wrote:
> If an object is destroyed by GC, and the object has a __del__, the __del__
> is called.
> 
[...]
> 
> What is true is that if an object *in* a trash cycle has a __del__, that
> cycle will not be collected, but rather shuffled into gc.garbage for the
> programmer to do something sane with it explicitly.  There may be any number
> of objects with __del__s not in cycles themselves but reachable only from
> cycles; they don't inhibit collection, and if collection occurs their
> __del__s will be invoked.  It's only the cases where Python can't possibly
> guess a safe order in which to call destructors where both __del__ and
> collection are inhibited.
> 
So what are the recommended practices if my program creates datastructures
containing cyclic references?

- avoid using __del__ in these objects by  cleaning them 'manually'?
- only using weak references to break these cycles?

- and finally using the debugflags of the gc module to ensure everything
is cleaned correctly?

Thomas




More information about the Python-list mailing list