epsilon = 0 S = 0 而epsilon< 10: S = S + epsilon epselon = epsilon + 1 print S From: Al*********************@p131.f3.n5025.z2.fidonet.o rg (Alexander Zatvornitskiy) Hello All! I''am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like ''var'' in Pascal, or special syntax in C. It can cause very ugly errors,like this: epsilon=0 S=0 while epsilon<10: S=S+epsilon epselon=epsilon+1 print S It will print zero, and it is not easy to find such a bug! 嗯。我肯定是编写错误代码的专家,但我不能说我在Python中犯了这个错误。为什么会这样? 我不确定,但有些事情可能会让我错过在实践中犯这个错误可能(在我的情况下有点非正式)单元测试 - 我测试至少在几个案例中得到正确的结果。 它也可能有助于使用Python我可以在更高的概念级别编码,或者可能只是有助于避免这些的语法问题:Hmmm. I am surely an expert in writing buggy code, but I can not say I make this error in Python. Why is that?I''m not sure, but a couple things that may help me miss making this mistakein practice may be (somewhat informal in my case) unit testing - I test for correct results for at least a few cases.It may also help that with Python I can code at a somewhat higher conceptual level, or maybe it is just the syntax that helps avoid these problems: for epsilon in range(0,10): S = S + epsilon for epsilon in range(0,10): for epsilon in range (0,10):S=S+epsilon for epsilon in range (0,10): S = S + epselon Traceback(最近一次调用最后一次): 文件"< pyshell#6>",第2行,在? S = S + epselon NameError:名称''epselon''未定义 看起来像跳下悬崖,但可读性的提高(变量声明是视觉混乱)使得它更容易让我看到我的代码,以及其中的任何拼写错误。 看起来很简单,让一个人的代码或其他脚本自动打印出一个排序列表变量 - 这会使你注意到的错误显而易见。但我至少还不需要这个。 您可能会喜欢Python并且发现缺少变量声明检查不是问题。它值得一试。S=S+epselonTraceback (most recent call last):File "<pyshell#6>", line 2, in ?S=S+epselonNameError: name ''epselon'' is not definedIt may seem like jumping off a cliff, but the improvement in readability (the variable declarations being visual clutter) makes it much easier for me to see my code, and any typos in it.It seems it would be simple enough to have one''s code, or another script, automatically print out a sorted list of the variables - which would make the error you note obvious. But I haven''t needed this, yet at least.You might like Python and find the lack of variable declaration checking not a problem. It''s worth a shot. Alexander Zatvornitskiy写道:Alexander Zatvornitskiy wrote: epsilon = 0 S = 0 而epsilon< 10: S = S + epsilon epselon = epsilon + 1 打印S 它会打印零,并且找到这样的错误并不容易! epsilon=0 S=0 while epsilon<10: S=S+epsilon epselon=epsilon+1 print S It will print zero, and it is not easy to find such a bug! pychecker可以帮助您找到拼写错误的变量名称。你必须将 代码移动到一个函数中,但是:pychecker may help you find misspelled variable names. You have to move thecode into a function, though: cat epsilon.py def loop(): epsilon = 0 S = 0 而epsilon< 10: S = S + epsilon epselon = epsilon + 1 print Scat epsilon.pydef loop():epsilon=0S=0while epsilon<10:S=S+epsilonepselon=epsilon+1print S 这篇关于变量声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!