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

bookChallenge: Decorator Chaining in Practice

Combining multiple decorators on a single function lets you layer several behaviors or transformations in a clear and reusable way. In real-world scenarios, you might want to log function calls, validate inputs, cache results, or modify outputsβ€”all without changing the core logic of your function. By chaining decorators, you can build up complex behavior step by step. The order in which you apply decorators matters, as each one wraps the result of the previous, leading to different outcomes depending on how you arrange them.

Task

Swipe to start coding

Create two decorators: one that adds 2 to the result of a function, and another that squares the result. Apply both decorators to the get_number function so that the final output reflects both transformations. The decorators should work regardless of the order in which they are applied.

  • The add_two_decorator must return the result of the function plus 2.
  • The square_decorator must return the square of the function result.
  • Both decorators must accept any arguments and keyword arguments passed to the function.
  • When both decorators are applied to get_number, the output must reflect both modifications in the correct order.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 4
single

single

Ask AI

expand

Ask AI

ChatGPT

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

Suggested prompts:

Can you give an example of combining multiple decorators?

How does the order of decorators affect the function's behavior?

What are some best practices for using multiple decorators?

close

bookChallenge: Decorator Chaining in Practice

Swipe to show menu

Combining multiple decorators on a single function lets you layer several behaviors or transformations in a clear and reusable way. In real-world scenarios, you might want to log function calls, validate inputs, cache results, or modify outputsβ€”all without changing the core logic of your function. By chaining decorators, you can build up complex behavior step by step. The order in which you apply decorators matters, as each one wraps the result of the previous, leading to different outcomes depending on how you arrange them.

Task

Swipe to start coding

Create two decorators: one that adds 2 to the result of a function, and another that squares the result. Apply both decorators to the get_number function so that the final output reflects both transformations. The decorators should work regardless of the order in which they are applied.

  • The add_two_decorator must return the result of the function plus 2.
  • The square_decorator must return the square of the function result.
  • Both decorators must accept any arguments and keyword arguments passed to the function.
  • When both decorators are applied to get_number, the output must reflect both modifications in the correct order.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 4
single

single

some-alt