这里的pos需要声明nonlocal,为什么new_pos不需要申明
来源:11-16 再用闭包解决一下_
Resun918
2019-09-13
origin = 0
def toutur(pos):
def go(step):
nonlocal pos
new_pos = pos + step
pos = new_pos
print(pos)
return pos
return go
tourist = toutur(origin)
tourist(2)
tourist(3)
tourist(6)
写回答
2回答
-
Resun918
提问者
2019-09-14
origin = 0 def toutur(pos): def go(step): nonlocal pos new_pos = pos + step pos = new_pos print(pos) return pos return go tourist = toutur(origin) tourist(2) tourist(3) tourist(6)
022019-09-16 -
7七月
2019-09-14
麻烦格式化下,不格式化看不懂代码。
012019-09-14
相似问题