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

Peter Otten __peter__ at web.de
Wed Feb 11 10:29:28 EST 2004


Stephen Ferg wrote:

> I've just spent several very frustrating hours tracking down a bug in
> one of my programs.  The problem was that I was writing text to a
> file, and when I was done I coded
> 
>    f.close
> 
> when I should have been coding
> 
>    f.close()
> 
> with the parentheses.
> 
> Although I love Python dearly, the fact that such an easy-to-make
> mistake should do nothing useful and do it silently (which, in this
> context, means: fail silently) seems to me a bit of a wart.
> 
> In any event, does anybody have any suggestions for how a coder could
> avoid making such a mistake, or detect it quickly?
> 
> I teach the occasional beginning Python class, and I would hate to
> have to tell my beginning students to watch out for making this kind
> of mistake because it can bite you in a most nasty way.

Try pychecker.

<pycheckerfodder.py>
if __name__ == "__name__":
    f = file("xxx")
    print f.readline(),
    f.close
</pycheckerfodder.py>

...> pychecker pycheckerfodder.py
Processing pycheckerfodder...

Warnings...

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

Peter



More information about the Python-list mailing list