>>> [i(4) for i in [(lambda n: i + n) for i in range(10)]]
and i got this: Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <lambda>
TypeError: unsupported operand type(s) for +: 'function' and 'int'
Apparently the outer and inter "i"s got confused. when i change the outer 'i' to 'f': [f(4) for f in [(lambda n: i + n) for i in range(10)]]
i get this: [13, 13, 13, 13, 13, 13, 13, 13, 13, 13]
With generator expressions it worked as expected: >>> [i(4) for i in ((lambda n: i + n) for i in range(10))]
[4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
Meanwhile: [i(4) for i in map(lambda i: lambda n:i+n, range(10))]
Also works as expected.This obviously has nothing to do with the lambda in python and everything to do with the way loops work.
Python has a few quirks of this kind, this one i didn't know about. I didn't know that list comprehensions and generator expressions differ in any other way than the thing they return. I should have known better, they are very different things.
edit: looks like this also works:
[i(4) for i in [(lambda i: lambda n:i+n)(i) for i in range(10)]]
Its all about scope I guess.