如何避免“f.close” (没有parens)错误? [英] How to avoid "f.close" (no parens) bug?

查看:172
本文介绍了如何避免“f.close” (没有parens)错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚花了几个非常令人沮丧的时间来追踪我的一个程序中的一个错误。问题是我正在写文件到

文件,当我完成后我编码了


f.close


当我应该编码时


f.close()


括号。

虽然我非常喜欢Python,但是这样一个易于制作的错误的事实应该没有任何用处并且无声地执行(其中,在这个

上下文,意思是:默默地失败)在我看来有点像疣。


无论如何,有没有人对编码器的建议有什么建议

避免犯这样的错误,或者快速检测到它?


我教偶尔开始上课,我讨厌

必须告诉我的开始学生要注意制作这种错误,因为它会以最恶劣的方式咬你。

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.

推荐答案

斯蒂芬弗格写道:
Stephen Ferg wrote:
我刚刚花了好几个f生锈时间追踪我的一个程序中的错误。问题是我正在写文件到
文件,当我完成后,我编码了

f.close

当我应该编码时/>
f.close()

括号。

虽然我非常喜欢Python,但这样一个易于制作的事实
错误应该没有任何用处并且默默地做(在这个上下文中,意思是:默默地失败)在我看来有点像疣。

无论如何,有没有人有关于编码人员如何避免犯这样的错误或快速检测出来的任何建议?

我教会偶尔开始Python课程,我讨厌
必须告诉我开始学生要注意制造这种错误,因为它会以最恶劣的方式咬你。
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.




尝试pychecker。


< pycheckerfodder.py>

if __name__ ==" __ name __":

f = file(" xxx")

打印f.readline(),

f.close

< /pycheckerfodder.py>


....> pychecker pycheckerfodder.py

处理pycheckerfodder ......


警告......


pycheckerfodder.py:4 :声明似乎没有效果


Peter



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




Stephen>我刚刚花了几个非常令人沮丧的时间来追踪一个

Stephen>我的一个程序中的错误。问题是我写的是

Stephen>文本到文件,当我完成后我编码


Stephen> f.close


Stephen>什么时候我应该编码


Stephen> f.close()


Stephen>括号。


Stephen>无论如何,有没有人对编码器的建议有什么建议

Stephen>可以避免犯这样的错误,或者很快发现它?


Pychecker可能会对此发出警告。给出这个四行文件:


def bar():

返回5


bar


Pychecker报道:


%pychecker foo.py

名称:foo

处理foo ......


警告......


foo.py:4:声明似乎无效

Skip


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


>斯蒂芬>我刚刚花了几个非常令人沮丧的时间追踪
> Stephen> I''ve just spent several very frustrating hours tracking down a
Stephen>我的一个程序中的错误。问题是我写的是斯蒂芬>文本到文件,当我完成后我编码

斯蒂芬> f.close

斯蒂芬>什么时候我应该编码

斯蒂芬> f.close()

斯蒂芬>括号。

斯蒂芬>在任何情况下,是否有人对编码器的方式有任何建议
Stephen>可以避免犯这样的错误,或者快速检测出来吗?

Pychecker可能会对此发出警告。鉴于这个四行文件:
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:




人们在使用Python进行编码时遇到的许多常见错误

编译器没有抓到。区分大小写的错误,

缩进等错误。


我认为PyChecker(
中提到.sf.net /target =_ blank> http://pychecker.sf.net/
,并且应该与每个Python集成IDE。

键入任何内容之前,请键入import pychecker.checker



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"


这篇关于如何避免“f.close” (没有parens)错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆