カレー化された関数のリストを作成しようとする試みがうまくいかないのはなぜですか?このpython関数のラムダ式はどうなっていますか?
def p(x, num):
print x, num
def test():
a = []
for i in range(10):
a.append(lambda x: p (i, x))
return a
>>> myList = test()
>>> test[0]('test')
9 test
>>> test[5]('test')
9 test
>>> test[9]('test')
9 test
ここでは何が起こっていますか?実際に私は上記の機能を行うことを期待し
機能は次のとおりです。
Pythonでimport functools
def test2():
a = []
for i in range (10):
a.append(functools.partial(p, i))
return a
>>> a[0]('test')
0 test
>>> a[5]('test')
5 test
>>> a[9]('test')
9 test
functools.partialを使用するソリューションがあるので、何が問題なのですか? –
質問は、最初の方法はなぜ機能しないのですか? – David