does python have useless destructors?

Tim Peters at
Mon Jun 14 10:37:03 CEST 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


    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


    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