块范围? [英] block scope?

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

问题描述

我有时会想念一件事,这在其他一些语言中很常见(c ++),

是块范围的概念。如果变量没有比b
更长,那么主要是为了避免名称冲突。这也导致了更加可读的代码。我想知道这是否已被讨论过?

One thing I sometimes miss, which is common in some other languages (c++),
is idea of block scope. It would be useful to have variables that did not
outlive their block, primarily to avoid name clashes. This also leads to
more readable code. I wonder if this has been discussed?

推荐答案

Neal Becker写道:
Neal Becker wrote:

我有时会想念一件事,这在其他一些语言中很常见(c ++),

是块范围的概念。如果变量没有比b
更长,那么主要是为了避免名称冲突。这也导致了更加可读的代码。我想知道是否已经讨论过这个问题了吗?
One thing I sometimes miss, which is common in some other languages (c++),
is idea of block scope. It would be useful to have variables that did not
outlive their block, primarily to avoid name clashes. This also leads to
more readable code. I wonder if this has been discussed?



可能,使用好的代码,块范围会有点矫枉过正,除了我

欢迎列表理解有一个新的范围:

pyi

-------------------------- ----------------------------------

Traceback(最近一次调用最后一次):

文件"< ipython console>",第1行,< module>

< type''exception.NameError''>:name' 我未定义


py [i for x in xrange(4)]

[0,1,2,3]

pyi#希望NameError

3

Probably, with good code, block scope would be overkill, except that I
would welcome list comprehensions to have a new scope:
pyi
------------------------------------------------------------
Traceback (most recent call last):
File "<ipython console>", line 1, in <module>
<type ''exceptions.NameError''>: name ''i'' is not defined

py[i for i in xrange(4)]
[0, 1, 2, 3]
pyi # hoping for NameError
3


James Stroud< js ***** @ mbi.ucla.eduwrites :
James Stroud <js*****@mbi.ucla.eduwrites:

或许,使用优秀的代码,块范围将是矫枉过正的,除非我将b / b
欢迎列表推导具有新的范围:
Probably, with good code, block scope would be overkill, except that I
would welcome list comprehensions to have a new scope:



块范围是一个胜利,因为它摆脱了是否
的不确定性
变量是否在块外使用。 好的代码理论

(只是手动不要在其他地方使用变量)在释放期限压力下并不总是持有

等等并且没有意义

无论如何。如果你不想要允许在内部块中创建变量并在

其他块中使用它们,那么不具有块范围的重点是什么?如果你想以这种方式使用它,我认为最好在相互封闭的范围内创建变量


Block scope is a win because it gets rid of the uncertainty of whether
the variable is used outside the block or not. The "good code" theory
(just manually don''t use the variable elsewhere) doesn''t always hold
up under release deadline pressure and so on and doesn''t make sense
anyway. What''s the point of NOT having block scope if you don''t want
to allow for creating variables in inner blocks and using them in
other blocks? I think it''s best to require creating the variable
in a mutually enclosing scope if you want to use it that way.


Paul Rubin写道:
Paul Rubin wrote:

James Stroud< js ***** @ mbi.ucla.eduwrites:
James Stroud <js*****@mbi.ucla.eduwrites:

>>可能,使用优秀的代码,块范围将是过度的,除了我欢迎列表推导有一个新的范围:
>>Probably, with good code, block scope would be overkill, except that I
would welcome list comprehensions to have a new scope:



块范围是一个胜利,因为它消除了变量是否在块外使用的不确定性。




Block scope is a win because it gets rid of the uncertainty of whether
the variable is used outside the block or not.



在声明很少的语言中,最好不要将
有太多不同的嵌套作用域。 Python在这个领域有一个合理的

折衷方案。函数和类有一个范围,但

" if"和用于不要。这很有效。


Javascript弄错了。他们有声明,但默认情况下,没有声明,是
是全局的,不是本地的,也不是错误的。

糟糕的设计。这是将声明改为语言的结果,

通常会产生痛苦的后遗症。


John Nagle

In a language with few declarations, it''s probably best not to
have too many different nested scopes. Python has a reasonable
compromise in this area. Functions and classes have a scope, but
"if" and "for" do not. That works adequately.

Javascript got it wrong. They have declarations, but the default,
in the absence of a declaration, is global, not local or an error.
Bad design. It''s a result of retrofitting declarations to a language,
which usually has painful aftereffects.

John Nagle


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

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