变量声明 [英] variable declaration

查看:77
本文介绍了变量声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!


我是python中的新手,我发现一个非常坏的东西(从我的角度来看)

语言。声明变量没有关键字或语法,比如

Pascal中的'var',或者C中的特殊语法。它可能会导致非常难看的错误,例如:

epsilon = 0

S = 0

而epsilon< 10:

S = S + epsilon

epselon = epsilon + 1

打印S


它将打印为零,并且找到这样的错误并不容易!


甚至Visual Basic都有''Option Explicit''关键字!可能是,python也有这样的功能,我只是不知道它吗?


Alexander, za ** @ bk.ru

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!

Even Visual Basic have ''Option Explicit'' keyword! May be, python also have such
a feature, I just don''t know about it?

Alexander, za**@bk.ru

推荐答案

> ------------原始消息------------
> ------------Original Message------------
来自:铝******************** *@p131.f3.n5025.z2.fidonet.o rg (Alexander Zatvornitskiy)

大家好!

我是python的新手,在
语言中,我发现了一个非常糟糕的事情(从我的观点来看)。没有关键字或语法来声明变量,例如Pascal中的'var''
,或者C中的特殊语法。它可能会导致非常难看的错误,例如
这个:
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+epselon

Traceback (most recent call last):
File "<pyshell#6>", line 2, in ?
S=S+epselon
NameError: name ''epselon'' is not defined
It 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 the
code into a function, though:


cat epsilon.py

def loop():

epsilon = 0

S = 0

而epsilon< 10:

S = S + epsilon

epselon = epsilon + 1

print S

cat epsilon.py
def loop():
epsilon=0
S=0
while epsilon<10:
S=S+epsilon
epselon=epsilon+1
print S


这篇关于变量声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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