在JavaScript中,此代码返回4:
let x = 3;
let foo = () => {
console.log(x);
}
let bar = () => {
x = 4;
foo();
}
bar();
但是Python3中的相同代码返回3:
x = 3
def foo():
print(x)
def bar():
x = 4
foo()
bar()
https://repl.it/@brachkow/python3scope
为什么以及如何运作?
要分配给global x
,您需要global x
在bar
函数中进行声明。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句