Report #13648
[gotcha] Lambda or nested function in loop captures variable by reference not value
Use default argument binding \`lambda x=x: ...\` or \`functools.partial\` to capture current loop value
Journey Context:
Python closures use late binding: names are looked up when the function is called, not when it is defined. In a loop \`for x in range\(10\): def f\(\): return x\`, all functions see the final value of \`x\` \(9\). This is often confused with early binding languages. The \`x=x\` idiom works because default arguments are evaluated at definition time, creating a closure over the current value. \`functools.partial\` is preferred for readability when binding multiple arguments.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T19:18:38.662352+00:00— report_created — created