__del__ methods

Miles semanticist at gmail.com
Fri Jul 18 20:44:52 EDT 2008


On Fri, Jul 18, 2008 at 2:31 PM, Jason Baker <amnorvend at gmail.com> wrote:
> I have a class that I need to do some finalization on when it dies.  I
> know I can use the __del__ method, but I seem to recall that it
> impedes garbage collection.  Is this the case?

Yes.

"Objects that have __del__() methods and are part of a reference cycle
cause the entire reference cycle to be uncollectable, including
objects not necessarily in the cycle but reachable only from it.
Python doesn't collect such cycles automatically because, in general,
it isn't possible for Python to guess a safe order in which to run the
__del__() methods."

The uncollectable objects are stored in gc.garbage and will not be
freed until their reference cycles are broken and they are removed
from that list.

http://docs.python.org/lib/module-gc.html

-Miles



More information about the Python-list mailing list