Discuss / Python / 作业作业

作业作业

Topic source

这次作业有点懵,看了评论写出来的,两个方法:

第一个方法:利用list

def createCounter():
    L = [0]
    def counter():
        L[0] +=1        return L[0]
    return counter

方法2:利用nonlocal

def createCounter():
    i = 0    def counter():
        nonlocal i
        i += 1        return i
    return counter

  • 1

Reply