这个python函数中的lambda表达式是怎么回事?
发布于 2021-01-29 14:10:47
为什么这种尝试创建咖喱函数列表的方法不起作用?
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
关注者
0
被浏览
95
1 个回答