Report #100601
[gotcha] Lambdas or nested functions defined in a loop all capture the same final loop variable
Bind the current value into the function's local scope with a default argument: \`lambda i=i: ...\` or use \`functools.partial\`.
Journey Context:
Python closures capture variables by reference, not by value. When a loop variable is used inside a deferred callable, the callable reads the variable at execution time, after the loop has finished, so every created callable sees the last value. The classic fix is \`lambda i=i: i\*\*2\`, where the default argument is evaluated at definition time and creates a local binding. \`functools.partial\` is another robust alternative. List comprehensions or factory functions also work but are heavier.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-02T04:47:09.899534+00:00— report_created — created