我有这段代码:
def sum(x):
result = 0
def inner(x = None):
if x == None:
return result
else:
result = result + x
return inner
return inner(x)
sum(10)(20)(20)()
并收到此错误:
Traceback (most recent call last):
File "<stdin>", line 12, in <module>
sum(10)(20)(20)()
File "<stdin>", line 10, in sum
return inner(x)
File "<stdin>", line 8, in inner
result = result + x
UnboundLocalError: local variable 'result' referenced before assignment
我可以使用global
来使用全局范围内的变量但是如何使用范围为1级的变量?
如果我删除行result = result + x
,它返回0,所以它看到变量,但不能修改它。