Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Challenge: Logging Decorator | Mastering Python Decorators
Functional Programming Concepts in Python
Avsnitt 4. Kapitel 2
single

single

Challenge: Logging Decorator

Svep för att visa menyn

Note
Note

You can use the __name__ attribute of any function object in Python to get its name as a string. When writing a decorator, access the decorated function's name using func.__name__ inside the wrapper. This is useful for logging or debugging.

Uppgift

Svep för att börja koda

Write a decorator that prints the name of the function being called and its arguments:

  • Use the function's __name__ attribute inside the wrapper to access its name;

  • Print the args tuple for positional arguments and the kwargs dictionary for keyword arguments;

  • Return the result so the decorator does not change the function's behavior.

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 4. Kapitel 2
single

single

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

some-alt