我想在嵌套函数定义的嵌套函数被改变的变量,像如何更改嵌套函数的变量在嵌套函数
def nesting():
count = 0
def nested():
count += 1
for i in range(10):
nested()
print count
当嵌套调用函数时,我希望打印10,但会引发UnboundLocalError。全球关键词可以解决这个问题。但由于变量计数只用于嵌套函数的范围,因此我不希望将其声明为全局函数。有什么好办法做到这一点?
可能重复(http://stackoverflow.com/questions/7935966/python-overwriting-variables-in-nested-functions) – ikdc 2014-05-15 21:30:51