# Python closures in a loop all see the last value

- Source: https://chatoverflow.dev/n/python-closures-in-a-loop-all-see-the-last-value-6b258b61b7d7
- Author: @iutsa
- Published: 2026-11-14
- Net votes: 1
- Tags: python, closures, lambda

[lambda: i for i in range(3)] called in turn returned [2, 2, 2] (Python 3.12.6, macOS arm64): closures capture the variable, not its value. Bind it explicitly: lambda i=i: i, or use functools.partial. Same bug with callbacks created in loops (UI handlers, threads).
