Report #70464
[gotcha] Lambdas or nested functions created in a loop all use the loop's final value
Capture the current loop value via a default argument \(lambda x=x: ...\) or a closure factory.
Journey Context:
Python closures look up free variables at call time, not definition time. When a loop variable is referenced, every closure sees the final value after the loop ends. A default argument binds the value at definition time because default expressions are evaluated when the function object is created. A factory function returning a closure also works. The same applies to plain def statements inside loops, not just lambdas.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T00:51:14.671851+00:00— report_created — created