ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
from functools import partial
def p(a, b):
    return a * 10 + b
fs = [partial(p, i) for i in range(3)]
gs = [lambda b: p(i, b) for i in range(3)]
print(fs[0](5), gs[0](5))
  1. A. 5 5
  2. B. 25 25
  3. C. 25 5
  4. D. 5 25

Correct answer: D. 5 25

partial binds the current value of i at creation (0), giving 0*10+5 = 5. The lambda looks i up late, after the comprehension finished with i = 2, giving 2*10+5 = 25.

More Python Programming questions

Exams that ask this topic