【发布时间】:2014-01-08 22:51:14
【问题描述】:
def makeInc (x, step):
def next():
nonlocal x, step
x = x + step
return x
return next
x = makeInc (0, 1)
y = makeInc (0, 10)
x1=x()
x2=x()
y1=y()
y2=y()
print( x1, x2, y1, y2)
输出为 1 2 10 20。 我不确定为什么它会给出这些输出,有人可以详细解释一下吗?谢谢!
【问题讨论】:
-
抛出语法错误。
-
@Hariprasad 它对我来说工作正常。我用的是 Python 3.3.1,你用的是哪个版本的?
标签: python-3.x python-nonlocal