What is the output of the following Python code snippet?
fs = [lambda: i for i in range(3)]
print([f() for f in fs])
print([f() for f in fs])
Correct answer
B. [2, 2, 2]
5 views
B. [2, 2, 2]
5 views
Late binding: each lambda looks up i when called, after the loop ended at 2.
Asked in: Python Interview Output Prediction
https://docs.python.org/3/reference/datamodel.htmlNot sure why the answer is B? Ask here and get an explanation.