Report #99671
[gotcha] Lambdas or closures inside a loop all capture the same final loop variable
Bind the loop variable as a default argument at definition time: \`lambda x=x: ...\` or use \`functools.partial\`. In comprehensions, use a generator that binds each value.
Journey Context:
Python's closures look up names when the function runs, not when it is created. A loop creates one scope, so every late-bound closure sees the variable's last value. A default argument is evaluated at definition time and stores the value in the function object, giving each closure its own copy.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-30T04:51:54.203533+00:00— report_created — created