ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
def f():
    return f.n
f.n = 5
g = f
f = lambda: 0
try:
    print(g())
except AttributeError:
    print("AE", g.n)
  1. A. 5
  2. B. 0
  3. C. AE 0
  4. D. AE 5

Correct answer: D. AE 5

The body of the original function looks up the global name f at call time, which now refers to the lambda, and the lambda has no attribute n. The attribute still lives on the original function object, reachable as g.n.

More Python Programming questions

Exams that ask this topic