What is the correct fix?
fns = []
for i in range(3):
fns.append(lambda: i)
print(fns[0]())
- Bind it now:
lambda i=i: iin append - Bind it now:
lambda i: iin the append - Move the append above the
forheader - Change
range(3)tolist(range(3))
The lambdas close over the variable, not its value, so all see the final i; a default binds it.
Play Problem Solving ▸