Editor

Predict the Decorator Output

Intermediate · Predict the output · from Python Decorators Explained: A Step-by-Step Guide

From the lesson: What Is the Basic Decorator Pattern?

A decorator is a function that takes a function as input, creates a wrapper function that adds behavior, and returns the wrapper. Here's the template:

Read this part of the lesson

What does this code print? Pay attention to the order of execution.

def shout(func):
    def wrapper(*args, **kwargs):
        result = func(*args, **kwargs)
        return result.upper()
    return wrapper

@shout
def greet(name):
    return f'hello, {name}'

print(greet('world'))

Write one print() statement that produces the exact same output.

# What does the decorated function print?
print("?")

More Decorator exercises

All Python exercises · 50 practice problems · Advanced Python quiz