def count():
fs = []
for i in range(1, 4):
def f():
return i*i
fs.append(f)
return fs
f1, f2, f3 = count()
print(f1(), f2(), f3())
I'm learning Python, finding the result above is 9 9 9
instead of 1 4 9
. I try to debug the code step by step, but in IDE I cannot get any extra information about <function count.<locals>.f at 0x0000000003987598>
when appending functions to list. I wanna know what is the detail order of the code, especially when appending f()
whether the value of the variable i
would be recorded(i*i or 1*1 or some other circumstance). And what 's the significance of for i in range(1, 4)
?