我有一份马提卜达职能清单,我想按顺序评估。 我不肯定为什么,但只有最后一项职能得到评价。 例如下:
>>> def f(x,z):
... print "x=",x,", z=",z
...
>>>
>>> g = lambda x : f(x,13)
>>> g(2)
x= 2 , z= 13 # As expected
>>>
>>> lst=[]
>>>
>>> for i in range(0,5):
... lst.append(lambda x: f(x,i))
...
>>> print lst
[<function <lambda> at 0x10341e2a8>, <function <lambda> at 0x10341e398>, <function <lambda> at 0x10341e410>, <function <lambda> at 0x10341e488>, <function <lambda> at 0x10341e500>]
>>>
>>> for fn in lst:
... fn(3)
...
x= 3 , z= 4 # z should be 0
x= 3 , z= 4 # z should be 1
x= 3 , z= 4 # z should be 2
x= 3 , z= 4 # z should be 3
x= 3 , z= 4 # as expected.
我认为,只有最后一点已经执行,而不是其他。 任何想法? 感谢!