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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 2

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

bookChaining Multiple Decorators

Swipe um das Menü anzuzeigen

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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 2
some-alt