1funcs = []
2for x in range(5):
3 funcs.append(lambda: x)
4
5print([f() for f in funcs])
6# Expected: [0, 1, 2, 3, 4]
7# Actual: [4, 4, 4, 4, 4]
no lines flagged
#017PracticeEasy10 min · 50 XP
Late Binding in Lambda Closures
A list of lambda functions all return the last value of the loop variable instead of their own captured value.
Flagged linesNo lines flagged yet
What's wrong?
Flag a line or write a note to submit.