Skip to main content
🔒 Preview mode. The first 15 Foundations lessons are free; this one is Pro. Start a 7-day trial to unlock the editor, AI hints and the rest of the curriculum. Card required, cancel any time in Dashboard.Start 7-day trial →
← CoursesSenior Deep-DivesModule 11 · Decorator patterns at scaleDecorator stacking — TOP wraps last, runs firstpredict151 / 161
+150 XP
Task
📝 **Task:** Predict the 8-line output. Three `@trace(...)` decorators stack on `work()`. Each wrapper prints `>{name}` on entry and `<{name}` on exit. Trace the call order: outermost enters first, innermost runs the real function, innermost exits first, outermost exits last. 📋 Implement the function above. Tests run automatically. 💡 **Hint:** Re-read the theory if you get stuck.
Predict the output

Read the code carefully

from functools import wraps


def trace(name: str):
    def decorator(fn):
        @wraps(fn)
        def wrapper(*args, **kwargs):
            print(f">{name}")
            result = fn(*args, **kwargs)
            print(f"<{name}")
            return result
        return wrapper
    return decorator


@trace("outer")
@trace("middle")
@trace("inner")
def work() -> str:
    print("work")
    return "ok"


result = work()
print(f"result: {result}")

What will the program print? Write here:

💬 Discussion

Be the first to ask a question or share a tip.
Sign in to join the discussion. Reading is free.
Loading discussion…