Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Chaining Multiple Decorators | Advanced Decorator Techniques
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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 2

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

bookChaining Multiple Decorators

Sveip for å vise menyen

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 2
some-alt