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 scaleParametrised decorators — three nested functions, not twopredict150 / 161
+150 XP
Task
📝 **Task:** Predict the 5-line output. A parametrised `@repeat(times=3)` decorator wraps `greet`. The wrapper runs the body 3 times per call. The final `__name__` check verifies that `@wraps(fn)` preserved the wrapped function's metadata. 📋 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 repeat(times: int):
    def decorator(fn):
        @wraps(fn)
        def wrapper(*args, **kwargs):
            result = None
            for _ in range(times):
                result = fn(*args, **kwargs)
            return result
        return wrapper
    return decorator


@repeat(times=3)
def greet(name: str) -> str:
    print(f"hi {name}")
    return name


result = greet("Ada")
print(f"returned: {result}")
print(greet.__name__)

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…