ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
def a(fn):
    def w(): return "a(" + fn() + ")"
    return w
def b(fn):
    def w(): return "b(" + fn() + ")"
    return w
@a
@b
def f(): return "f"
print(f(), f.__name__)
  1. A. a(b(f)) w
  2. B. b(a(f)) w
  3. C. a(b(f)) f
  4. D. b(a(f)) f

Correct answer: A. a(b(f)) w

Stacked decorators apply bottom-up: f = a(b(f)), so the outermost wrapper is a's. Without functools.wraps the result is just the inner function named w.

More Python Programming questions

Exams that ask this topic