python-函数-闭包-loop中没有域-02

版权声明:左右代码均为自己总结,若有雷同请勿模仿 https://blog.csdn.net/weixin_44253023/article/details/89634182

def fun():
a=[]
for i in range(1,4):
def func():
return i**2
a.append(func)
return a

list=fun()
for f in list:
a=f()
print(a)

#使用闭包过程中,一旦外函数被调用一次返回了内函数的引用
#每次调用内函数,是开启一个函数执行过后死亡
#但是闭包变量只有一份
#每次开启内函数都在使用同一份闭包变量

猜你喜欢

转载自blog.csdn.net/weixin_44253023/article/details/89634182