does python have useless destructors?

Michael P. Soulier msoulier at
Thu Jun 10 02:51:43 CEST 2004

On Wed, 09 Jun 2004 15:00:32 -0700, Donn Cave <donn at>
> On the other hand, while we can't completely resolve this
> problem, I think the text you quota above errs in trying
> to paint it as a non-problem.  try..finally is certainly
> not a convenient substitute for guaranteed finalization.
> The immediate finalization normally supported in Python
> is a very powerful, elegant programming feature.  For just
> the same reasons that automatic control of memory allocation
> is powerful and elegant -- memory is one of the resources
> your objects use.
> So yes, you can depend on immediate finalization in many
> common situations, but you have to accept the liability of
> a dependence on reference non-circularity and on the C Python.

As soon as I heard about not being able to trust destructors, I was
shocked, because I do rely on guaranteed finalization, and I also
consider it to be very elegant. If the only situations where I cannot
trust it are with Jython, and circular references, then I have no issue.

However, consider the code...

myfile = open("myfilepath", "w")

If the write fails due to a lack of disk space, the exception will
prevent the explicit close() from happening. Now, if myfile goes out of
scope, I would hope that the file object is destroyed and thus the file
is closed, but after reading the python documentation, it seems that
the only way to guarantee closure of the file is using the mentioned
try/finally construct...

    myfile = open("myfilepath", "w")

Also, there is the case of Python not guaranteeing the __del__ method of
an object being called if the object is still alive when the process
exits. For files, I don't care, since the filehandles will be returned
by the OS, but for some external resources, this would be a big issue.
FIFOs and shared memory segments, for example. 


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