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

Peter Hansen peter at engcorp.com
Thu Feb 12 08:55:10 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.

One thing no one has done here yet is to ask about your background
in programming.

Would I be write in guessing that you have used VisualBASIC a lot
in the past?  Or perhaps a similar language which allows the type
of function call you would expect with the first form above?

I have *never* made this particular mistake in Python, nor, as 
far as I can tell, have any of the several dozen Python programmers
I've hired over the past few years (none of whom have VB backgrounds).

I believe f.close could be a legal call in VB (am I right?) and 
I'm guessing that is the reason you are making this mistake.
I doubt it's a common problem for many people, but maybe it is for
those with a particular background.

-Peter



More information about the Python-list mailing list