利用nonlocal改变外部参数:
def createCounter(): n = 0 def counter(): nonlocal n n += 1 return n return counter 利用可变的list:
def createCounter(): n = [0] def counter(): n[0] += 1 return n[0] return counter
每次调用createCounter() n=[0]不是都会重新初始化吗,为什么是动态的..求个解释
Sign in to make a reply
Pmz丶
利用nonlocal改变外部参数:
def createCounter(): n = 0 def counter(): nonlocal n n += 1 return n return counter 利用可变的list:
def createCounter(): n = [0] def counter(): n[0] += 1 return n[0] return counter