ProgrammingHardIBM · Deloitte pattern

What is printed?

What is printed?
def decorator_with_args(prefix):
    def actual(f):
        def wrapper(*a, **k):
            return prefix + str(f(*a, **k))
        return wrapper
    return actual
@decorator_with_args("result: ")
def add(a, b):
    return a + b
print(add(2, 3))
  1. A. 5
  2. B. result: 5
  3. C. TypeError
  4. D. add(2, 3)

Correct answer: B. result: 5

decorator_with_args('result: ') is called first and returns actual, the real decorator, which wraps add so every call prefixes the string 'result: ' onto the stringified sum: 'result: 5'.

More Python Programming questions

Exams that ask this topic