ProgrammingHardTCS NQT · Infosys

What does this Python code print? def deco(tag): def wrap(fn): def inner(x): return "<" + tag + ">" + fn(x) + "</" + tag + ">" return inner return wrap @deco("b") @deco("i") def show(x): return x print(show("hi"))

  1. A. <i><b>hi</b></i>
  2. B. <b>hi</b><i>hi</i>
  3. C. <b><i>hi</b></i>
  4. D. <b><i>hi</i></b>

Correct answer: D. <b><i>hi</i></b>

Decorators apply bottom-up: show = deco("b")(deco("i")(show)). The inner (i) wrapper wraps the original function, and the b wrapper wraps that result, so b is the outermost tag.

More Python Programming questions

Exams that ask this topic