闭包的应用
'''
1. 保存返回返回闭包时的状态(外层函数值的状态)
'''
def func(a,b):
c = 10
def inner_func(): # 创建一个内部函数就占一个地址,扔出来的是不会被回收的
s = a+b+c
print('相加之后的结果是:',s)
print(locals())
return inner_func
# 调用func
# func(6,9)()
ifunc = func(6,9) # ifunc变量就是inner_func ifunc = inner_func
ifunc1 = func(2,8)
ifunc2 = func(1,9)
print(ifunc)
print(ifunc1)
print(ifunc2)
ifunc1()
ifunc()
ifunc2()
'''
当调用func(6,9)---》a=6,b=9--->inner_func()内部函数中-->返回内部函数
(此时返回的内部函数已经记录此时的a和b的值,所所以不会受到a,b改变的影响)
再次调用func(2,8)--》a=2,b=8---》保存在本次的inner_func()函数中,---》返回
的时候返回的就是保存2,8内部函数
'''