3 ms·
How does list comprehension change anything here? This has the same problem: add_n = [lambda x: x + n for n in range(10)] add_n[9](10) # 19 add_n[
by panzi 3y ago
How does list comprehension change anything here? This has the same problem:
add_n = [lambda x: x + n for n in range(10)]
add_n[9](10) # 19
add_n[0](10) # 19
- billyjmc 3y agoI’m not sure what they mean by list comprehensions, either, but for completeness’s sake, I must point out that this is solvable by adding `n` as a keyword argument defaulting to `n`: add_n = [lambda x, n=n: x + n for n in range(10)] add_n[9](10) # 19 add_n[0](10) # 10
- drekipus 3y agoThis is the way Also pylsp warns you about this