Forum
>>
Programmazione Python
>>
Database
>>
Decoratori Python: registrazione personalizzata per chiamate di funzioni
Pagina: 1
Esegui il login per scrivere una risposta.
Pagina: 1
Scritto da Mobo01 |
2023-09-07 13:13:50 - Decoratori Python: registrazione personalizzata per chiamate di funzioni
|
I'm working on a Python project where I have multiple functions and I want to log each function call along with its arguments and return the value for debugging purposes. I've heard that decorators can help achieve this. Could someone guide me on how to create a custom decorator for logging function calls?
Here's what I have in mind: def log_function_call(func): def wrapper(*args, **kwargs): # Log function call, arguments, and return value result = func(*args, **kwargs) # Log the result return result return wrappers @log_function_call def add(a, b): return a + b @log_function_call def subtract(a, b): return a - b # Example function calls result1 = add(5, 3) result2 = subtract(10, 4) I would like to log function calls and their results in a clean and organized way. How can I modify the log_function_call decorator to achieve this? Also, what is the best procedure to setup the logger in Python to capture these logs effectively? Any code examples or recommendations would be greatly appreciated. Thank you! |
|
Scritto da ㎝ |
2023-09-07 14:37:31 - Re: Decoratori Python: registrazione personalizzata per chiamate di funzioni
|
potresti cominciare da qui:
https://www.w3resource.com/python-exercises/decorator/python-decorator-exercise-1.php THE 🍺-WARE LICENSE (Revision ㊷):
<㎝🐌🐍.🇮🇹> wrote this post. As long as you retain this notice you can do whatever you want with this stuff. If we meet some day, and you think this stuff is worth it, you can buy me a 🍺 in return. -- ㎝ |
Pagina: 1
Esegui il login per scrivere una risposta.