【发布时间】:2017-03-07 14:46:49
【问题描述】:
def func():
def nested():
global x
x = 1
x = 2
func()
print(x)
正确答案是“2”,原因是 func() 没有定义。但是当我读到这个时,似乎 func() 被定义为nested()。我认为当你调用 func() 时,它会自动调用nested()。我正在努力掌握这一点并理解为什么我不应该那样阅读它。
【问题讨论】:
-
nested被创建但未被调用。
标签: python scope nested-function