Skip to content
Onsite

Output: lambdas created in a loop, each returning the loop variable, called after the loop

MediumP2Output

What does this code print?

1funcs = []
2for i in range(3):
3 funcs.append(lambda: i)
4print([f() for f in funcs])
5
6fixed = [lambda i=i: i for i in range(3)]
7print([f() for f in fixed])
Saved automatically

One line per print, as Python prints it. An uncaught exception as its last traceback line, for example ZeroDivisionError: division by zero.

Hints

Saved automatically