does python have useless destructors?
dkturner at telkomsa.net
Fri Jun 18 10:43:59 CEST 2004
Isaac To <kkto at csis.hku.hk> wrote in message news:<7ifz8udxp2.fsf at enark.csis.hku.hk>...
> >>>>> "David" == David Turner <dkturner at telkomsa.net> writes:
> David> So are we to take it that efficiency considerations are a serious
> David> impediment to a potentially valuable safety feature?
> that reference counting is used. But for any portable code, one must be
> more careful---exactly our current situation. We need no more discussion
> about how to collect garbages. On the other hand, PEP 310 discusses
> something completely unrelated, which still should attract some eyeballs.
We have three issues here - garbage collection (undoubtedly more
efficient than reference counting), RAII (undoubtedly safer than
try/finally), and PEP 310 (the "with" form).
I've already pointed out the problems with "with" (it's the same ball
game as try/finally, and suffers from the same limitations). So my
opinion is that the RAII idiom should somehow be enabled in Python.
The crucial difference between "with" and RAII here is that "with"
requires intervention by the end-user of the object, whereas the RAII
idiom does not.
The most obvious way in which RAII can be enabled is by introducing
defined reference-counting semantics for objects with __del__. I
can't think of another way of doing it off-hand, but there must be
others (if reference counting has unacceptably high overhead).
One possibility is to introduce scoped objects, analogous to auto or
stack-allocated objects. It would be illegal to pass a reference to
these outside of a given scope, and they would have __del__ (or
another method) called when the scope ends. This is slightly cheaper
than reference counting, but of course it imposes very real
limitations on the usefulness of the object. However, this pattern
might still have its uses.
Any other suggestions would be most welcome...
More information about the Python-list