Report #11275
[gotcha] exec\('x = 1'\) in function scope doesn't change local variable x
Use a mutable container \(dict/list\) passed to exec's locals to capture results, or move exec to module scope where globals and locals are the same; do not rely on exec to modify function locals directly.
Journey Context:
CPython optimizes function locals into a fixed-size array indexed by slot, not a dict. \`locals\(\)\` returns a snapshot copy, so modifications are ignored. \`exec\` with explicit locals dict works, but that dict won't update the function's actual local variables because the interpreter has already bound the local names to slots at compile time.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T12:53:18.683530+00:00— report_created — created