[issue26361] lambda in dict comprehension is broken

2016-02-14 Thread Eryk Sun
Eryk Sun added the comment: 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 pa

[issue26361] lambda in dict comprehension is broken

2016-02-14 Thread Steven D'Aprano
Steven D'Aprano added the comment: 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 v

[issue26361] lambda in dict comprehension is broken

2016-02-14 Thread Samuel Ainsworth
Samuel Ainsworth added the comment: Also applies to list comprehensions. For example, lst = [(lambda x: x * t) for t in range(5)] lst[0](1) # => 4 -- ___ Python tracker ___ _

[issue26361] lambda in dict comprehension is broken

2016-02-14 Thread Samuel Ainsworth
New submission from Samuel Ainsworth: 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. -- messages: 260287 nosy: Samuel.Ainsworth priority: normal severity: normal status: open title: lambda in dict comp