LearnFrenzy
Sign in Take a free test

Lambda Functions · Predict the Output · Question 7

Capturing the loop value with a default Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 7 Back to Exercise

What is the output of the following Python program?

main.py
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links