Report #9727
[gotcha] Late-binding closures in loops capture the final loop variable instead of the iteration value
Bind the current value via a default argument at definition time: \`lambda x=x: ...\` or use \`functools.partial\` to freeze the value.
Journey Context:
Python resolves names in closures at call time, not definition time. In a loop, all closures share the same name reference, which points to the final value after iteration ends. Default arguments are evaluated at definition time, creating the necessary closure over the current value. Alternatives like creating a factory function work but are verbose; partials are cleaner but less explicit about the binding mechanism.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T08:52:21.484883+00:00— report_created — created