【发布时间】:2010-10-24 21:38:58
【问题描述】:
为什么这种创建柯里化函数列表的尝试不起作用?
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
这是怎么回事?
实际上我期望上面的函数做的一个函数是:
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 的解决方案,那么问题是什么?
-
问题是,为什么第一种方法不起作用?