Question 7
Back to Exercise
What is the output of the following Python program?
fs = [lambda i=i: i * 10 for i in range(3)]
print([f() for f in fs], fs[1](7))
- A [20, 20, 20] 70
- B [0, 10, 20] 70 Correct
- C [0, 10, 20] 10
- D TypeError
Correct Answer: Option B
Defaults are evaluated when each lambda is created, freezing 0, 1 and 2. Passing an argument overrides the default: 7 * 10.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!