[Python-3000] Removing __del__

Jim Jewett jimjjewett at gmail.com
Sun Sep 24 02:38:40 CEST 2006


On 9/22/06, Bob Ippolito <bob at redivi.com> wrote:

> I still haven't seen one that can't be done pretty trivially
> with a weakref.  Perhaps the solution is to make
> doing cleanup-by-weakref easier or more obvious?

Possibly, but I've tried, and *I* couldn't come up with any way to use
them that was

(1) generic enough to put in a module, rather than a recipe
(2) easy enough to still be an improvement, and
(3) correct.

>     def __call__(self, object, func, *args, **kw):
>         def cleanup(ref):
>             self.refs.remove(ref)
>             func(*args, **kw)
>         self.refs.add(weakref.ref(object, cleanup))

Now remember something like Michael Chermside's "simplest" example,
where you need to flush before closing.

The obvious way is to pass self.close, but it doesn't actually work.
Because it is a bound method, it silently makes the object effectively
immortal.

The "correct" way is to write another function which is basically an
awkward copy of self.close.  At the moment, I can't think of any
*good* way to ensure access to self.resource1 and self.resource2, but
not to self.  All the workarounds I can come up with make __del__ look
pretty good, from a maintenance standpoint.

-jJ


More information about the Python-3000 mailing list