Report #98271
[gotcha] Lambdas or nested functions created in a loop all capture the final loop value
Bind the current value as a default argument at definition time: \`lambda x=x: ...\` or use \`functools.partial\(f, x\)\`.
Journey Context:
Python closures close over variable names, not values. Every function created in a loop refers to the same \`i\` or \`x\`, which ends up as the last value after the loop. A default argument is evaluated when the function is defined, freezing the value for each iteration. This is the standard pattern for callbacks, GUI handlers, and lists of lambdas.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-27T04:41:04.781028+00:00— report_created — created