Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Challenge: Decorate Multiple Functions | Decorator Fundamentals
Python Decorators Explained

bookChallenge: Decorate Multiple Functions

Decorators in Python are designed to be reusable, meaning you can apply the same decorator to many different functions. This flexibility allows you to add the same behavior—such as logging, timing, or validation—to multiple functions without repeating code. By defining a decorator once and then applying it wherever needed, you keep your codebase clean and consistent. When a decorator is applied to more than one function, each function will receive the additional functionality provided by the decorator, but the core logic of each function remains unchanged. This approach is especially powerful for cross-cutting concerns like debugging or monitoring, where you want to add the same action to various parts of your program.

Aufgabe

Swipe to start coding

Create a decorator named log_function_name that logs the name of any function it decorates before the function is called. Apply this decorator to both greet and add. When either function is called, the decorator must print the function's name before executing the function and returning its result.

Lösung

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 4
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

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

close

bookChallenge: Decorate Multiple Functions

Swipe um das Menü anzuzeigen

Decorators in Python are designed to be reusable, meaning you can apply the same decorator to many different functions. This flexibility allows you to add the same behavior—such as logging, timing, or validation—to multiple functions without repeating code. By defining a decorator once and then applying it wherever needed, you keep your codebase clean and consistent. When a decorator is applied to more than one function, each function will receive the additional functionality provided by the decorator, but the core logic of each function remains unchanged. This approach is especially powerful for cross-cutting concerns like debugging or monitoring, where you want to add the same action to various parts of your program.

Aufgabe

Swipe to start coding

Create a decorator named log_function_name that logs the name of any function it decorates before the function is called. Apply this decorator to both greet and add. When either function is called, the decorator must print the function's name before executing the function and returning its result.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 4
single

single

some-alt