does python have useless destructors?
Tim Peters
tim.one at comcast.net
Mon Jun 14 04:37:03 EDT 2004
[David Turner]
...
> The D programming language somehow contrives to have both garbage
> collection and working destructors.
Copied from the D docs:
The garbage collector is not guaranteed to run the destructor for all
unreferenced objects. Furthermore, the order in which the garbage
collector calls destructors for unreference [sic] objects is not
specified.
...
Allocating class instances on the stack is useful for temporary objects
that are to be automatically deallocated when the function is exited.
No special handling is needed to account for function termination via
stack unwinding from an exception. To work, they must not have
destructors.
...
When the garbage collector calls a destructor for an object of a class
that has members that are references to garbage collected objects, those
references are no longer valid. This means that destructors cannot
reference sub objects. This rule does not apply to auto objects or
objects deleted with the DeleteExpression.
> So why can't Python?
It's too hard to come up with an implementation so full of subtle but
crucial distinctions <wink>.
More information about the Python-list
mailing list