Challenge: 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.
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_decoratormust return the result of the function plus 2. - The
square_decoratormust 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.
Lösung
Danke für Ihr Feedback!
single
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Awesome!
Completion rate improved to 5.88
Challenge: Decorator Chaining in Practice
Swipe um das Menü anzuzeigen
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.
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_decoratormust return the result of the function plus 2. - The
square_decoratormust 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.
Lösung
Danke für Ihr Feedback!
single