<分区>
在下面的代码中,
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
?