Report #101053
[gotcha] Lambda or closure defined in a loop captures the loop variable, not its value
Bind the current value as a default argument: funcs = \[lambda x=i: x for i in range\(3\)\], or use functools.partial.
Journey Context:
Python's late-binding closures look up free variables by name at call time, not definition time. In a loop, every closure therefore sees the final value of the loop variable. A default argument is evaluated at definition time, so it freezes the value for that iteration. functools.partial and named inner functions work too, but the default-argument trick is the most compact and widely recognized fix. The FAQ calls this out explicitly because it is the classic 'all callbacks use the last index' bug.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-06T04:54:37.067000+00:00— report_created — created