ProgrammingHardIBM · Deloitte pattern

What is printed?

What is printed?
def outer_deco(f):
    print("setup")
    def wrapper(*a, **kw):
        print("call")
        return f(*a, **kw)
    return wrapper

@outer_deco
def hello():
    print("hello")

hello()
hello()
  1. A. setup call hello call hello
  2. B. setup setup call hello call hello
  3. C. call hello call hello setup
  4. D. setup call hello

Correct answer: A. setup call hello call hello

'setup' runs exactly once, when the decorator is applied at definition time. Each call to hello() then re-enters wrapper, printing 'call' followed by 'hello' every time.

More Python Programming questions

Exams that ask this topic