在 Harmony 中,有一个 let
关键字允许声明作用域为最近 block 的变量。例如
function foo() {
if (true){
let a = 100;
}
return a
}
自 a
起将导致错误仅在 if
内定义 block 。
我知道我可以使用del
实现同样的事情,但是这是手动的而不是像let
那样自动的关键字
最佳答案
Python 为每个类、模块、函数或生成器表达式创建作用域。代码块内没有范围。您也许可以使用嵌套函数来实现您的预期目标,例如:
def outside():
def inside():
var=5
print var
外部调用将导致:
outside()
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "<input>", line 5, in outside
NameError: global name 'var' is not defined
关于javascript - Python 中是否有类似于 Harmony 的 let 关键字的东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24414988/