Discuss / Python / 感谢评论区的各位

感谢评论区的各位

Topic source

dudefitslol

#1 Created at ... [Delete] [Delete and Lock User]

def createCounter():

    i = 0

    def counter():

        nonlocal i  #这句声明是闭包函数应用同名变量的重点。

        while True:

            i = i + 1

            return i

    return counter

Pangzhi74890

#2 Created at ... [Delete] [Delete and Lock User]

为啥要有while True呀

为啥不能用global 把n定义为全局变量,nonlocal却可以运行


  • 1

Reply