>>> <function num.<locals>.<listcomp>.<lambda> at 0x00000217771D3D90> <function num.<locals>.<listcomp>.<lambda> at 0x00000217771D3E18> <function num.<locals>.<listcomp>.<lambda> at 0x00000217771D3EA0>
发现对函数进行for循环返回三个内存地址不同的返回值,判定和range(3)有关系
1 2 3 4 5 6 7 8
defnum(): return [lambda x:i*x for i in range(1)]
for m in num(): print(m)
>>> <function num.<locals>.<listcomp>.<lambda> at 0x0000011C3EA73D90>
defnum(): return [lambda x:i*x for i in range(4)] #将3改成4
a = [m(2) for m in num()] print(a)
>>> [6, 6, 6, 6]
发现由4变成6,推断正确,range(4)最大是3,三乘二得六,都是以最后一个作为返回值。
全部都返回一个数的原因:
1 2 3 4 5
a = [lambda x:x*i for i in range(3)] print(a)
>>> [<function <listcomp>.<lambda> at 0x00000288BD48C268>, <function <listcomp>.<lambda> at 0x00000288BDE24D90>, <function <listcomp>.<lambda> at 0x00000288BDE24E18>, <function <listcomp>.<lambda> at 0x00000288BDE24EA0>]
直接生成四个函数,由此可见for m in num()只不过是对这个列表进行遍历。然后再将参数传入每个函数再生成一个列表。此时所有的i都已经是3了。
使用如下改法可以产生不同效果:
1 2 3 4 5 6 7 8
defnum(): return [lambda x:[i*x for i in range(3)]]