数的累加的另一种代码(闭包)
''' 数的累加的另一种代码写法(闭包) ''' origin = 0 def factroy(pos): def go(step): nonlocal pos ## 声明不是本地变量 new_pos = pos + step pos = new_pos return new_pos return go tourist = factroy(origin) ##实例化对象 for i in range(1,101): s = tourist(i) print(s)
''' 数的累加的另一种代码写法(闭包) ''' origin = 0 def factroy(pos): def go(step): nonlocal pos ## 声明不是本地变量 new_pos = pos + step pos = new_pos return new_pos return go tourist = factroy(origin) ##实例化对象 for i in range(1,101): s = tourist(i) print(s)