473,382 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

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.
Jul 18 '05 #1
12 2127
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
Jul 18 '05 #2

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

Jul 18 '05 #3
DH
> 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:


There are numerous common mistakes people making when coding in Python
that the compiler doesn't catch. Case-sensitive errors, errors in
indenting, etc.

I think PyChecker ( http://pychecker.sf.net/ ) should be mentioned in
every beginner tutorial, and should be integrated with every Python IDE.
Before you type anything, type "import pychecker.checker"
Jul 18 '05 #4
"Stephen Ferg" <st***@ferg.org> wrote in message
news:b1**************************@posting.google.c om...
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.


I don't know about anyone else, but I'd appreciate a run
time error message when it detects an impossible operation
on a function/bound method/unbound method which has no /
one operand on the top of the stack.

Something simple, like: "Unsupported operation on {function
| unbound method | bound method} with {no | one} operand
detected. Did you forget the empty calling parameter list < () >?"

So far, that would have saved me close to a cumulative
person-week of work!

John Roth
Jul 18 '05 #5
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
Jul 18 '05 #6
On Thu, 12 Feb 2004 08:55:10 -0500, Peter Hansen <pe***@engcorp.com>
wrote:
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.

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 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.

Pascal, Delphi also use this convention. Personally, I think that
design for a language is strange, but if the language doesn't support
a "pointer to function" or "function" type, there's no ambiguosity to
the compiler. A casual reader of the code can get confused, not
knowing if the code is referring to a function that returns a value,
or a simple attribute. Delphi complicates it further by having
"attributes", which are sort of like a getter/setter used
(syntactically) as if it were an attribute assignment/reference.
Point being it's not just VisualBasic (and Access and VBA). I
think it's not unreasonable that your thought about someone's
background is relevant. I'm from a C background, and never made this
particular mistake.
--dang
Jul 18 '05 #7
Peter Hansen wrote:
...
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 make this mistake from time to time although by now I am more fluent
in Python than any other language (and I was never fluent in a language
where you called functions without parens). I also leave off trailing
colons, quotes, etc. It is just laziness. I suspect I would never write:

f.close

(it does stand out like a sore thumb)

But I might write

foo(bar[0].doSomething)

I can usually diagnose these pretty quickly.

Paul Prescod

Jul 18 '05 #8
Paul Prescod wrote:

But I might write

foo(bar[0].doSomething)


Good point. Given that this is much more likely to be deliberate,
it's also more likely to occur as a mistake for those of us who
would "never" write f.close accidentally.

I also leave off the odd colon, though I believe that's the only
such error I've made more than maybe once with Python. Never left
quotes off anything that was supposed to have them, unless it was
strictly a slip of the finger (instead of the mind).

-Peter
Jul 18 '05 #9
Peter Hansen <pe***@engcorp.com> pisze:
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?


You call Pascal "similar to VB"?

--
Jarek Zgoda
http://jpa.berlios.de/
Jul 18 '05 #10
Jarek Zgoda wrote:

Peter Hansen <pe***@engcorp.com> pisze:
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?


You call Pascal "similar to VB"?


Yes, in at least this once sense, obviously. Clearly no other similarity
with VB is needed to suffice as a background for someone who makes such
a mistake "commonly", if my theory is correct.

-Peter
Jul 18 '05 #11
>>>>> "Dang" == Dang Griffith <no*****@noemail4u.com> writes:

Dang> I'm from a C background, and never made this particular mistake.

At least in GCC, the compiler will emit a warning when you do that (provided
that you use -Wall), telling you that you've generated a value without using
it. This isn't possible in Python (or did I miss something?), so you never
know how many such subtle problem lies in your program, especially because
forgetting to close a file "normally" won't cause any visible problem (that
is, until you modify the same file somewhere elso and when the program
exits, half of your modifications are gone).

Regards,
Isaac.
Jul 18 '05 #12
Peter Hansen <pe***@engcorp.com> wrote:
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.


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


As a long time Perl programmer I've found myself making exactly that
mistake in Python. In Perl method calls don't need parens if the call
doesn't take any arguments.

However I've also discovered pychecker ;-)

--
Nick Craig-Wood
nc*@axis.demon.co.uk
Jul 18 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: felciano | last post by:
Hi -- I am trying to use the csv module to parse a column of values containing comma-delimited values with unusual escaping: AAA, BBB, CCC (some text, right here), DDD I want this to come...
32
by: KK | last post by:
Hello all, I have a unsigned char buffer 'buffer' and I need to convert the first 12 bytes of it into a string. Below is a code that should work, however, how can I avoid reinterpret_cast...
2
by: tibirna | last post by:
Hello mem_fun_ref(&(std::list<int>::size)) is compiled as valid by gcc 3.3.4 and causes an error ("defined-id needed") with gcc 4.0.2. The later requires mem_fun_ref(&std::list<int>::size)...
7
by: red floyd | last post by:
Given class C { int m; public: int& func() { return m; } // other elements redacted for brevity }; Other than the fact that functions that return a handle are bad, is
2
by: Terry Olsen | last post by:
I have an SQL database with rows that have parens in the data. If I run a select statement such as: SELECT SongName FROM Songs WHERE SongName = 'John Jacob (Jingleheimer Schmidt)' It...
3
by: Peter Michaux | last post by:
Hi, In the following string I would like to find the word that comes after "test" as long as test is not inside parenthesis. In this example the match would be "two". "the (test one) test...
6
by: howa | last post by:
Since it is part fo the standard, why I always heard that we should avoid iframe? any comments? thanks.
5
by: Kevin | last post by:
Hi! Is there anyway to avoid flicking between pages navigation in Asp.Net 2.0, like smartnavigation atribute in asp.net 1.1? Thanks and Regards.
11
Niheel
by: Niheel | last post by:
http://bytes.com/images/howtos/information_overloaded.jpgPaul Graham wrote an interesting article a few months back about how the internet is leading to information overload for information workers...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.