does python have useless destructors?

Michael P. Soulier msoulier at
Wed Jun 9 22:40:11 CEST 2004


I recently had a friend tell me that because the Python spec does not
guarantee that an object will necessary be destroyed when all references
to it are gone, that desctructors in Python are not reliably called, and
thus useless. As I found it difficult to believe that Python would
include destructors for apparently no reason, I thought I should ask for
clarification here.

Can anyone say yay or nay on this issue? 

I believe his comments stem from a couple of references.

Specifically, this paragraph in the second link:

    Some objects contain references to ``external'' resources such as
    open files or windows. It is understood that these resources are
    freed when the object is garbage-collected, but since garbage
    collection is not guaranteed to happen, such objects also provide an
    explicit way to release the external resource, usually a close()
    method. Programs are strongly recommended to explicitly close such
    objects. The `try...finally' statement provides a convenient way to
    do this.

So putting a close of some external resource in a destructor would be a
bad idea, apparently. As this is the kind of thing I typically use
destructors in OO programming for, I found this quite surprising.


Michael P. Soulier <msoulier at>
The major advances in civilization are processes that all but wreck the
societies in which they occur.
-- Albert North Whitehead

More information about the Python-list mailing list