[Python-3000] Removing __del__

Jim Jewett jimjjewett at gmail.com
Tue Sep 26 15:30:01 CEST 2006

On 9/26/06, Tim Peters <tim.peters at gmail.com> wrote:
> [Giovanni Bajo]
> > You say that __del__ is harardous, but I can't see how
> > weakrefs are less hazardous. As an implicit finalization method, they live on
> > the fragile assumption that the callback won't hold a reference to the object:

> Nope, not so.

I think you read "live" as "not trash", but in this particular
sentence, he meant it as "be useful".

> Read Modules/gc_weakref.txt for the gory details.  In
> outline, there are three objects of interest here:  the weakly
> referenced object (WO), the weakref (WR) to the WO, and the callback
> (CB) callable attached to the WR.

> /Normally/ the CB is reachable (== not trash).

(Otherwise it can't act as a finalizer, because it isn't around)

> If a reachable CB has
> a strong reference to the WO, then that keeps the WO reachable too,

So it doesn't act as a finalizer; it acts as an immortalizer.  All the
pain of __del__, and it takes only one to make a loop.  (Bound methods
are in this category.)

> OTOH, if the CB has a weak reference to the WO, then when the WO goes
> away and the CB is invoked, the CB's weak reference returns None
> instead of the WO.

So it still can't act as a proper finalizer, if only because it isn't
fast enough.


More information about the Python-3000 mailing list