Discuss / Python / 不知道nonlocal,一直在想怎么用子函数改变母函数的变量

不知道nonlocal,一直在想怎么用子函数改变母函数的变量

Topic source
def createCounter():
    n = 0
    def counter():
        nonlocal n
        n = n + 1
        return n
    return counter

singcao

#2 Created at ... [Delete] [Delete and Lock User]
def createCounter():
    s = [0]
    def counter():
        s[0] = s[0] + 1
        return s[0]
    return counter

  • 1

Reply