python 由表及里理解 lambda
作者:互联网
def multipliers():
return [lambda x: i * x for i in range(4)]
print([m(2) for m in multipliers()])
# [6, 6, 6, 6]
multipliers内嵌套了一个匿名函数
该匿名函数引用外部非全局变量i
返回该嵌套函数
def multipliers():
# 添加了一个默认参数i=i
return [lambda x, i=i: i * x for i in range(4)]
print([m(2) for m in multipliers()])
# [0, 2, 4, 6]
标签:return,python,multipliers,range,print,由表及里,def,lambda 来源: https://www.cnblogs.com/jade-north/p/14877770.html