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

Skip Montanaro skip at pobox.com
Wed Feb 11 10:15:12 EST 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

    bar

Pychecker reports:

    % pychecker foo.py
    name: foo
    Processing foo...

    Warnings...

    foo.py:4: Statement appears to have no effect

Skip




More information about the Python-list mailing list