Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Chaining Multiple Decorators | Advanced Decorator Techniques
Python Decorators Explained

bookChaining Multiple Decorators

1234567891011121314151617
def bold_decorator(func): def wrapper(*args, **kwargs): return "<b>" + func(*args, **kwargs) + "</b>" return wrapper def italic_decorator(func): def wrapper(*args, **kwargs): return "<i>" + func(*args, **kwargs) + "</i>" return wrapper @bold_decorator @italic_decorator def greet(): return "Hello!" print(greet()) # Output: <b><i>Hello!</i></b>
copy

When you chain multiple decorators on a single function, Python applies them from the closest decorator to the function outward, but executes them in the reverse order. In the provided example, @italic_decorator is applied to greet first, then @bold_decorator wraps the result. This means that when you call greet(), the call first goes through bold_decorator, which then calls the version of greet that has already been wrapped by italic_decorator. As a result, the output is enclosed first in <i>...</i>, then in <b>...</b>, producing <b><i>Hello!</i></b>. Understanding this order is crucial when combining multiple decorators, as the sequence can affect the final behavior and output of your function.

question-icon

Fill in the blanks to show the correct order in which decorators are applied and executed.

  • What should go in the blanks ___A___ and ___B___ so that "First decorator" is printed before "Second decorator" when calling say_hi()?

  • Type your answers as: first_decorator, second_decorator

  • A:

  • B:

,
First decorator
Second decorator
Hi!

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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. 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

bookChaining Multiple Decorators

Scorri per mostrare il menu

1234567891011121314151617
def bold_decorator(func): def wrapper(*args, **kwargs): return "<b>" + func(*args, **kwargs) + "</b>" return wrapper def italic_decorator(func): def wrapper(*args, **kwargs): return "<i>" + func(*args, **kwargs) + "</i>" return wrapper @bold_decorator @italic_decorator def greet(): return "Hello!" print(greet()) # Output: <b><i>Hello!</i></b>
copy

When you chain multiple decorators on a single function, Python applies them from the closest decorator to the function outward, but executes them in the reverse order. In the provided example, @italic_decorator is applied to greet first, then @bold_decorator wraps the result. This means that when you call greet(), the call first goes through bold_decorator, which then calls the version of greet that has already been wrapped by italic_decorator. As a result, the output is enclosed first in <i>...</i>, then in <b>...</b>, producing <b><i>Hello!</i></b>. Understanding this order is crucial when combining multiple decorators, as the sequence can affect the final behavior and output of your function.

question-icon

Fill in the blanks to show the correct order in which decorators are applied and executed.

  • What should go in the blanks ___A___ and ___B___ so that "First decorator" is printed before "Second decorator" when calling say_hi()?

  • Type your answers as: first_decorator, second_decorator

  • A:

  • B:

,
First decorator
Second decorator
Hi!

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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 2
some-alt