闭包——计数函数调用次数

要求最终效果类似
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())
posted @ 2020-03-20 16:34  pythoner_wl  阅读(248)  评论(0编辑  收藏  举报