[Python-3000] Removing __del__

Bob Ippolito bob at redivi.com
Sat Sep 23 02:35:50 CEST 2006


On 9/22/06, Aahz <aahz at pythoncraft.com> wrote:
> On Sat, Sep 23, 2006, Giovanni Bajo wrote:
> >
> > Did you actually read my posts where I have shown some legitimate use
> > cases of __del__ which can't be substituted with short and elegant
> > enough code?
>
> The question is whether those use cases are frequent enough -- especially
> for less-than-wizard programmers -- to warrant keeping __del__ around.

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? Something like this maybe:

import weakref

class GarbageDisposal:
    def __init__(self):
        self.refs = set()

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

on_cleanup = GarbageDisposal()

class Wrapper:
    def __init__(self, *args):
        self.handle = CAPI.init(*args)
        on_cleanup(self, CAPI.close, self.handle)

    def foo(self):
        CAPI.foo(self.handle)

-bob


More information about the Python-3000 mailing list