#闭包=函数+环境变量(函数定义时候)
#现场
def curve_pre():
a=25
def curve(x):
return a*x*x
return curve
f=curve_pre()
print(f(2))
# 100
#
# Process finished with exit code 0
origin=0
def factory(pos):
def go(step):
nonlocal pos
new_pos=pos+step
pos=new_pos
return new_pos
return go
tourist=factory(origin)
print(tourist(2))
print(tourist.__closure__[0].cell_contents)
print(tourist(3))
print(tourist.__closure__[0].cell_contents)
print(tourist(5))
print(tourist.__closure__[0].cell_contents)
# 2
# 2
# 5
# 5
# 10
# 10
# Process finished with exit code 0