Report #8735
[gotcha] Lambda or closure in loop captures variable by reference not value
Bind the current iteration value using a default argument \`lambda x=i: ...\` or use \`functools.partial\`
Journey Context:
Python closures close over the variable name, not the value at definition time. In a loop \`for i in range\(3\): ...\`, all created lambdas see the final value of \`i\` \(2\) because they share the same scope. Binding via default argument \`x=i\` evaluates \`i\` at lambda definition time, capturing the value. \`functools.partial\` is another way to freeze arguments. This is a common pitfall with list comprehensions creating callbacks or threads.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T06:17:22.283998+00:00— report_created — created