Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Decorator Syntax and Application | Decorator Fundamentals
Python Decorators Explained

bookDecorator Syntax and Application

1234567891011
def uppercase_decorator(func): def wrapper(): result = func() return result.upper() return wrapper @uppercase_decorator def greet(): return "hello, world" print(greet())
copy

The @ symbol in Python is used to apply a decorator to a function in a clear and concise way. When you place @decorator_name directly above a function definition, Python automatically passes the function below to the decorator as an argument. This is exactly the same as writing function = decorator_name(function), but the @ syntax is more readable and is the standard way to apply decorators.

Decorators are applied at the time the function is defined, not when it is called. This means that as soon as Python encounters the decorated function in your code, it wraps the original function with the decorator, replacing the original function with the result returned by the decorator. As a result, the name of the function actually refers to the decorated version, not the original.

One important detail is that unless you take special steps (which you will learn later), applying a decorator can change the function's name and metadata. After decoration, the function’s __name__ will usually be the name of the wrapper function inside the decorator, not the original function name.

question-icon

Fill in the blanks so that the @exclaim_decorator is correctly applied to the say_hello function.

exclaim_decorator def say_hello(): return "Hello" print(say_hello()) # Output: Hello!
Hello!

Click or drag`n`drop items and fill in the blanks

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 2

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

bookDecorator Syntax and Application

Scorri per mostrare il menu

1234567891011
def uppercase_decorator(func): def wrapper(): result = func() return result.upper() return wrapper @uppercase_decorator def greet(): return "hello, world" print(greet())
copy

The @ symbol in Python is used to apply a decorator to a function in a clear and concise way. When you place @decorator_name directly above a function definition, Python automatically passes the function below to the decorator as an argument. This is exactly the same as writing function = decorator_name(function), but the @ syntax is more readable and is the standard way to apply decorators.

Decorators are applied at the time the function is defined, not when it is called. This means that as soon as Python encounters the decorated function in your code, it wraps the original function with the decorator, replacing the original function with the result returned by the decorator. As a result, the name of the function actually refers to the decorated version, not the original.

One important detail is that unless you take special steps (which you will learn later), applying a decorator can change the function's name and metadata. After decoration, the function’s __name__ will usually be the name of the wrapper function inside the decorator, not the original function name.

question-icon

Fill in the blanks so that the @exclaim_decorator is correctly applied to the say_hello function.

exclaim_decorator def say_hello(): return "Hello" print(say_hello()) # Output: Hello!
Hello!

Click or drag`n`drop items and fill in the blanks

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 2
some-alt