$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · hard · #148

What is the correct fix?

callbacks = [lambda: i for i in range(3)]
print(callbacks[0]())
  1. Bind i as a default argument
  2. Wrap each lambda in tuple()
  3. Replace the lambda with def
  4. Iterate over list(range(3))
The lambdas close over i itself, so all three see its final value.
Play Problem Solving ▸