nonlocal 访问变量

def counter(start = 0):
    def incr():
        nonlocal start  #分别保存每一个变量的临时值、类似yield
        start += 1
        return start
    return incr
c1 = counter(5)
print(c1())
c2 = counter(50)
print(c2())
# c1 继续上次,输出接下来的值
print(c1())
print(c2())

2020-05-08

原文地址:https://www.cnblogs.com/hany-postq473111315/p/12846915.html