Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn 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

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain what would happen if I reversed the order of the decorators?

How do decorators work with functions that take arguments?

Can you show an example with more than two decorators?

bookChaining Multiple Decorators

Swipe to show 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

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2
some-alt