闭包——计数函数调用次数
要求最终效果类似
print(couter()) # 1
print(couter()) # 2
print(couter()) # 3
print(couter()) # 4
print(couter()) # 5
def couter():
x = 0
def counter():
nonlocal x
x += 1
return x
return counter
couter1 = couter()
print(couter1())
print(couter1())
print(couter1())
print(couter1())
print(couter1())