does python have useless destructors?
David Turner
dkturner at telkomsa.net
Mon Jun 14 03:51:10 EDT 2004
Carl Banks <imbosol at aerojockey.invalid> wrote in message news:<g31zc.89734$DG4.42241 at fe2.columbus.rr.com>...
> [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.
Regards
David Turner
More information about the Python-list
mailing list