Report #14998
[gotcha] Lambda or nested function in loop captures variable reference instead of value
Bind the current iteration value as a default argument: lambda x=x: ... or use functools.partial to freeze arguments
Journey Context:
Python uses late binding for closures: names are looked up when the function is called, not when it is defined. The loop variable is mutated during iteration, so all closures see the final value. Default arguments are evaluated at definition time, capturing the current loop value. functools.partial provides an alternative for regular functions that doesn't expose the binding in the signature.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T22:53:26.836681+00:00— report_created — created