【发布时间】:2017-12-21 12:05:11
【问题描述】:
在下面的代码中,
def makeAverage():
series = []
def average(newValue):
series.append(newValue)
total = sum(series)
return total/len(series)
return average
python 解释器不期望 series 在 average() 中是 nonlocal。
但是在下面的代码中
def makeAverage():
count = 0
total = 0
def average(newValue):
nonlocal count, total
count += 1
total += newValue
return total/count
return average
问题:
为什么python解释器期望count和total在average()中声明nonlocal?
【问题讨论】:
标签: python python-3.x closures python-nonlocal