Discuss / Python / 在闭包中 内部函数对外部函数作用域中的变量进行修改 需要nonlocal

在闭包中 内部函数对外部函数作用域中的变量进行修改 需要nonlocal

Topic source

Carbon

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

  • 1

Reply