的家庭,我想產生一系列相似的這個簡單的例子lambda函數:生成的lambda函數
fns = [(lambda x: x == y) for y in range(10)]
結果我從這個得到確實的10個功能列表。但是,所有10個似乎都必須是9,這是序列的最後一個值。例如
[fns[i](9) for i in range(10)] --> [True, True, True, True, True, True, True, True, True, True]
fns[0](0) --> False
爲什麼不工作,以及乾淨的解決方法是什麼?
我已經在Python 2.7和3.3中試過了。
將lambda定義更改爲'(lambda x,y = y:x == y)' – thefourtheye