This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: lambda in dict comprehension is broken
Type: behavior Stage: resolved
Components: Versions: Python 3.5, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Samuel.Ainsworth, eryksun, steven.daprano
Priority: normal Keywords:

Created on 2016-02-14 23:09 by Samuel.Ainsworth, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg260287 - (view) Author: Samuel Ainsworth (Samuel.Ainsworth) Date: 2016-02-14 23:09
If we construct a dict as follows,

x = {t: lambda x: x * t for t in range(5)}

then `x[0](1)` evaluates to 4! Tested on 2.7 and 3.5.
msg260288 - (view) Author: Samuel Ainsworth (Samuel.Ainsworth) Date: 2016-02-14 23:14
Also applies to list comprehensions. For example, 

lst = [(lambda x: x * t) for t in range(5)]
lst[0](1)   # => 4
msg260289 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2016-02-15 00:17
This is the standard behaviour of closures in Python. It's annoying, and often not what you expect, but it's not a bug.

Effectively, your dict or list contains five functions, each of which refer to the same variable "t". By the time the loop finishes, that variable has the value 4, so naturally all five functions see the same value for t.

The standard work-around is to use the default-argument trick to take a snapshot of the current value of the variable at the moment the function is created:

[(lambda x, t=t: x * t) for t in range(5)]
msg260290 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2016-02-15 01:43
For Python 3 you can also make it a keyword-only argument by adding a bare '*' to the parameter list:

    funcs = [(lambda x, *, t=t: x * t) for t in range(5)]

Code that accidentally calls funcs[0](3, 'silent bug ') will raise a TypeError because "t" can only be passed as a keyword argument.

An alternative is to use another lambda instead of using a default argument:

    funcs = [(lambda y: (lambda x:  x * y))(t) for t in range(5)]

This lets you continue to use a closure (now over the temporary scope of the outer call) and keep the function signature free of extra arguments. However, using a keyword-only argument is obviously less obfuscated.
History
Date User Action Args
2022-04-11 14:58:27adminsetgithub: 70549
2016-02-15 01:43:46eryksunsetnosy: + eryksun
messages: + msg260290
2016-02-15 00:17:25steven.dapranosetstatus: open -> closed

nosy: + steven.daprano
messages: + msg260289

resolution: not a bug
stage: resolved
2016-02-14 23:14:51Samuel.Ainsworthsetmessages: + msg260288
2016-02-14 23:09:13Samuel.Ainsworthcreate