def count():
fs = []
for i in range(1, 4):
def f():
return i*i
fs.append(f)
return fs
f = count()[0]
print(f())
f1, f2, f3 = count()
print(f1())#9
print(f2())#9
#都是9的原因:因为fs[]得到第三个函数的时候i=3了,当调用f1()的时候函数f才开始真正执行
#此时,i不变,i=3
def count():
fs=[]
def f(i):
def g():
return i*i
return g
for i in range(1,4):
fs.append(f(i))
return fs
f = count()[0]
print(f())
f1, f2, f3 = count()
print(f1())#1
print(f2())#4
C:\Users\Administrator.000\AppData\Local\Programs\Python\Python36\python.exe E:/python/09匿名函数.py
9
9
9
1
1
4
Process finished with exit code 0