does python have useless destructors?
dkturner at telkomsa.net
Sun Jun 13 19:24:14 CEST 2004
Carl Banks <imbosol at aerojockey.invalid> wrote in message news:<ebVyc.87665$DG4.6027 at fe2.columbus.rr.com>...
> David Turner wrote:
> > Well-defined destructor semantics have proven to be a robust,
> > reliable, and surprisingly general solution to a wide range of
> > problems. It wouldn't be *that* hard to implement them in Python - so
> > why not adopt a good idea?
> Wrong. It would be almost impossible in Python.
> Python can never rely on someone not taking a reference to your
> resource-owning object. Anyone can store an open file object in a
> list, for example, and there might even be good reasons to do it.
> Thus, you can never guarantee that the garbage collector will pick up
> the object when the function exits, even if it were perfect.
Carl, I'm sorry, but you've *completely* missed the point here.
(a) I've stated several times already that we're *not* talking about
__del__ being called if/when the object is garbage collected. Garbage
collection/finalization are completely different things to
(b) So what if multiple references to a deterministic object can be
made? It really doesn't matter. The point is that the destruction of
the object is still predictable.
> OTOH, if you indiscreetly finalize (for example, calling close on a
> file object) any resource-owning object as soon as the function that
> created it exits, not waiting for garbage collector to pick it up,
> then you've pretty much stepped on the toes of someone else
That is *not* what I suggested. Please read my suggestion again.
> It seems to me that you are failing to taking the possibility that
> these thing can happen into account. But, in Python, they can and do.
> Finalizing an object as soon as the name binding it goes out of scope
> is not always the right thing to do.
I never said it was.
I'm not going to bother responding to the rest of your post. Please
read my original suggestion again. In case you can't find it, here is
yet another precis:
Objects that define __del__ shall have a reference count, which is
incremented when names are bound to the object and decremented when
those names go out of scope. The __del__ method is called when the
reference count reaches zero. This mechanism is orthogonal to garbage
More information about the Python-list