delete and circular references

Alex Martelli aleaxit at yahoo.com
Thu Oct 26 12:31:01 EDT 2000


"Paul-Michael Agapow" <p.agapow at ic.ac.uk> wrote in message
news:1ej470d.1n14tyn1lheimN%p.agapow at ic.ac.uk...
>
> I can't find an answer to this in the documentation so: what's the best
> way to handle circular references with an object that is about to be
> deleted? It is unclear to me exactly what __del__ () does, whether it is
> a deletion or pre-deletion handler.

It is called when the _last_ reference count to the object has
been taken away.  Therefore, it's about special actions you may
want to take when the object itself is about to disappear (you
don't need to 'del' its fields, for example -- their reference
counts will be decremented anyway when the object is gone).

I suspect you'll have to introduce a specific removal-operation
for those objects that you know are subject to cyclic references,
such as:

> For example: I have a class that is a tree data structure. As I wish to
> traverse up and down the branches fast, the nodes contain references to
> their parent and their child nodes. Hence the circularity:

Alternatively, *AVOID* defining a __del__, and rely on Python 2
garbage collection of cyclic structures (which gets disabled for
objects which do define __del__).


Alex






More information about the Python-list mailing list