ProgrammingHardPython · GATE DA · Advanced

What is printed?

What is printed?
def deco(tag):
    def w(f):
        def g(*a):
            return f'<{tag}>' + f(*a) + f'</{tag}>'
        return g
    return w
@deco('b')
@deco('i')
def t(s): return s.upper()
print(t('hi'), t.__name__)
  1. A. <i><b>HI</b></i> g
  2. B. <b><i>HI</i></b> g
  3. C. <i><b>HI</b></i> t
  4. D. <b><i>HI</i></b> t

Correct answer: B. <b><i>HI</i></b> g

Decorators apply bottom-up, so 'i' wraps the function first and 'b' wraps that, giving <b><i>HI</i></b>; without functools.wraps the name is the wrapper's name g.

More Python Programming questions

Exams that ask this topic