[Python-3000] Removing __del__

Adam Olsen rhamph at gmail.com
Wed Sep 20 00:31:36 CEST 2006


On 9/19/06, Giovanni Bajo <rasky at develer.com> wrote:
> Michael Chermside <mcherm at mcherm.com> wrote:
>
> > Since we're apparently still in "propose wild ideas" mode for Py3K
> > I'd like to propose that for Py3K we remove __del__. Not "fix" it,
> > not "tweak" it, just remove it and perhaps add a note in the manual
> > pointing people to the weakref module.
>
>
> I don't use __del__ much. I use it only in leaf classes, where it surely can't
> be part of loops. In those rare cases, it's very useful to me. For instance, I
> have a small classes which wraps an existing handle-based C API exported to
> Python. Something along the lines of:
>
> class Wrapper:
>     def __init__(self, *args):
>            self.handle = CAPI.init(*args)
>
>     def __del__(self, *args):
>             CAPI.close(self.handle)
>
>     def foo(self):
>             CAPI.foo(self.handle)
>
> The real class isn't much longer than this (really). How do you propose to
> write this same code without __del__?

I've experimented with using metaclasses to do some fun here.  It
could look something like this:

Class Wrapper(Core):
    def __init__(self, *args):
        Core.__init__(self)
        self.core.handle = CAPI.init(*args)

    @coremethod
    def __coredel__(core):
        CAPI.close(core.handle)

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

Works just fine in 2.x.

-- 
Adam Olsen, aka Rhamphoryncus


More information about the Python-3000 mailing list