Discuss / Python / 闭包

闭包

Topic source

def createCounter():

    x = 0

    def counter():

        nonlocal x # 声明x不是本层函数的变量

        x = x + 1

        return x

    return counter

counterA = createCounter()


  • 1

Reply