does python have useless destructors?

David Turner dkturner at
Mon Jun 14 09:51:10 CEST 2004

Carl Banks <imbosol at aerojockey.invalid> wrote in message news:<g31zc.89734$DG4.42241 at>...
> [snip]
> > I don't need to know whether my function is the sole user of an object
> > and it falls to me to free it when I'm done, because the system takes
> > care of that.  I get it, I use it, I forget about it. 
> The problem is, you can't always afford to forget about it.  Sometimes
> you have to make sure that at this point in the program, this resource
> has been released.
> If you're relying on garbage collection to do that for you, you're
> asking for trouble.

Carl, this is why we're suggesting a mechanism other than garbage
collection to deal with this.  It's very handy to wrap a resource in
an object and put the acquire() in __init__ and the release() in
__del__.  But this only works if we know when __del__ is going to be
called.  Therefore, when an object explicitly declares __del__ (which
happens very rarely), it should use a reference counting mechanism to
decide when to call __del__ *as opposed to calling __del__ when the
object is garbage collected*.

This is exactly how CPython works at the moment, bar one exceptional
case which needs to be fixed.

David Turner

More information about the Python-list mailing list