Predict Stacked Decorator Output
Advanced · Predict the output · from Python Decorators Explained: A Step-by-Step Guide
From the lesson: What Happens When You Stack Multiple Decorators?
You can apply multiple decorators to a single function. They're applied bottom-up (closest to the function first), but they execute top-down when the function is called.
Read this part of the lessonWhat does this code print?
from functools import wraps
def add_prefix(func):
@wraps(func)
def wrapper(*args, **kwargs):
return '>>> ' + func(*args, **kwargs)
return wrapper
def add_suffix(func):
@wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs) + ' <<<'
return wrapper
@add_prefix
@add_suffix
def message():
return 'hello'
print(message())Write one print() statement with the exact output.
# What does message() return?
print("?")
More Decorator exercises
- Predict the Decorator OutputIntermediate
- Write a Logging DecoratorAdvanced
- Write a Repeat DecoratorAdvanced
- Write a Validation DecoratorAdvanced
- Write a Retry DecoratorAdvanced
- Fix the Broken DecoratorIntermediate
- Build a Simple Cache DecoratorAdvanced
All Python exercises · 50 practice problems · Advanced Python quiz