Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Chaining Multiple Decorators | Advanced Decorator Techniques
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
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

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 2

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

bookChaining Multiple Decorators

Veeg om het menu te tonen

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

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 2
some-alt