[Python-3000] PEP: Eliminate __del__

Jean-Paul Calderone exarkun at divmod.com
Sat May 5 18:27:08 CEST 2007


On Sat, 05 May 2007 13:21:59 +0200, Giovanni Bajo <rasky at develer.com> wrote:
>On 04/05/2007 20.35, Adam Olsen wrote:
>
>> Any attempt that keeps the entire contents of __dict__ alive is
>> doomed.  It's likely to contain a cycle back to the original object,
>> and avoiding that is the whole point of jumping through these hoops.
>
>Uh? If __dict__ contains a cycle back to the original object, then the object
>is part of a cycle already, with or without getting an additional reference to
>the __dict__ within the finalization callback.

If the __dict__ contains a cycle back to the original object, then if you
keep the __dict__ alive in the weakref callback (which is what you are doing
if the weakref callback references the __dict__ - it does not weakly
reference it), then you will keep the original object alive and the weakref
callback will never run, because the original object will live forever.

Contrariwise, if the weakref callback has only a reference to the particular
objects which it needs, then it doesn't matter if there is a cycle through
some _other_ objects which are in the __dict__, since the weakref callback
will not keep the cycle alive: eventually the cyclic gc will clean up the
cycle (but leave the objects referenced by the weakref callback alone, since
the weakref callback is keeping them alive), then the weakref callback will
run since it was a weakref to the original object which has now been
collected, the weakref callback will be able to use the specific references
it has to do some cleanup, and then most likely both the weakref object, the
weakref callback object, and whatever specific objects it held references to
will be dropped and eventually collected (though this is not necessarily the
case, since the weakref callback could choose to keep the specific objects it
referenced (not the object weakly referenced in the first place) alive by
putting them into some other living container).

Jean-Paul


More information about the Python-3000 mailing list