How to avoid "f.close" (no parens) bug?

Skip Montanaro skip at
Wed Feb 11 16:15:12 CET 2004

    Stephen> I've just spent several very frustrating hours tracking down a
    Stephen> bug in one of my programs.  The problem was that I was writing
    Stephen> text to a file, and when I was done I coded

    Stephen>    f.close

    Stephen> when I should have been coding

    Stephen>    f.close()

    Stephen> with the parentheses.

    Stephen> In any event, does anybody have any suggestions for how a coder
    Stephen> could avoid making such a mistake, or detect it quickly?

Pychecker would probably have warned about this.  Given this four-line file:

    def bar():
        return 5


Pychecker reports:

    % pychecker
    name: foo
    Processing foo...

    Warnings... Statement appears to have no effect


More information about the Python-list mailing list