Question
Option 1 : [2, 2, 2]
Detailed Solution
सही उत्तर [2, 2, 2] है।
Question:
The Python code below builds a list of lambdas inside a loop and calls them afterwards. What list is printed? funcs = [lambda: i for i in range(3)]; print([f() for f in funcs]) / नीचे दिया गया Python कोड लूप के भीतर लैम्ब्डाओं की सूची बनाता है और बाद में उन्हें कॉल करता है। कौन-सी सूची प्रिंट होगी? funcs = [lambda: i for i in range(3)]; print([f() for f in funcs])
Correct Answer: [2, 2, 2]
The lambdas capture the variable i by reference (late binding), not its value at creation time. When called after the loop, i is 2 for all of them, so each returns 2. / लैम्ब्डा चर i को संदर्भ द्वारा (लेट बाइंडिंग) कैप्चर करते हैं, निर्माण समय के मान द्वारा नहीं। लूप के बाद कॉल करने पर सभी के लिए i का मान 2 होता है, अतः प्रत्येक 2 लौटाता है।