[Python-Dev] __del__ and tp_dealloc in the IO lib

rdmurray at bitdance.com rdmurray at bitdance.com
Fri Jan 23 15:57:02 CET 2009


On Fri, 23 Jan 2009 at 11:57, Giovanni Bajo wrote:
> The fact that file objects are collected and closed immediately in all
> reasonable use cases (and even in case of exceptions, that you mention,
> things get even better with the new semantic of the except clause) is a
> *good* property of Python. I regularly see people *happy* about it.

I have never assumed that python closed my files before the end of the
program unless I told it to do so, and have always coded accordingly.
To do otherwise strikes me as bad coding.  I don't believe I ever
considered that such an assumption was even thinkable:  closing open files
when I'm done with them is part of my set of "good programming" habits
developed over years of coding, habits that I apply in _any_ language in
which I write code.  (In fact, it took me a while before I was willing
to let python take care of closing the files at program end...and even
now I sometimes close files explicitly even in short programs.)

Closing file objects is a specific instance of a more general programming
rule that goes something like "clean up when you are done".  I do in
general trust python to clean up python data structures because it knows
better than I do when "done" arrives; but when I know when "done" is,
I do the cleanup.  I love the 'with' statement :)

--RDM


More information about the Python-Dev mailing list