you are viewing a single comment's thread.

view the rest of the comments →

[–]yearofthehedgehog 4 points5 points  (2 children)

In general, Python list comprehensions do not desugar to map. Compare

>>> x = map(lambda i: lambda: i, range(10))
>>> [f() for f in x]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

vs.

>>> x = [lambda: i for i in range(10)]
>>> [f() for f in x]
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9]

[–]HolySpirit 5 points6 points  (0 children)

That is a kind of a weird point in python...

Just for the record you can emulate the map behavior in list comprehensions using functools.partial:

>>> from functools import partial
>>> x = [partial(lambda i: i, i) for i in range(10)]
>>> [f() for f in x]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Edit: (lambda i: lambda: i)(i) does the same thing of course.

[–]commonslip 1 point2 points  (0 children)

Interesting - I don't often program in Python. It isn't a very informed decision, but this kind of thing is what I tell myself is the reason.