Issue 26361: lambda in dict comprehension is broken (original) (raw)

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:27 admin set github: 70549
2016-02-15 01:43:46 eryksun set nosy: + eryksunmessages: +
2016-02-15 00:17:25 steven.daprano set status: open -> closednosy: + steven.dapranomessages: + resolution: not a bugstage: resolved
2016-02-14 23:14:51 Samuel.Ainsworth set messages: +
2016-02-14 23:09:13 Samuel.Ainsworth create