如何在python3中访问上限值?

坏了:

在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

为什么以及如何运作?

AKX:

要分配给global x,您需要global xbar函数中进行声明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章