From 5ddf93ae0c9f3c78225272262bb87d707681172b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20Adolfo=20Mej=C3=ADa=20S=C3=A1nchez?= Date: Fri, 14 Nov 2025 20:37:00 -0500 Subject: [PATCH] Add wrapper with arguments extend the wrapper example with arguments --- python.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/python.md b/python.md index 017ff3bb86..8beee58d5b 100644 --- a/python.md +++ b/python.md @@ -1107,6 +1107,28 @@ my_function(1,2) # => "Entering function my_function" print(my_function.__name__) # => 'my_function' print(my_function.__doc__) # => 'Adds two numbers together.' + +# Wrapper with argument + +def log_function_with_args(log_level): # wrap your log_function into a new one to set log_level as argument + def log_function(func): + @wraps(func) + def wrapper(*args, **kwargs): + print(f"{log_level}: Entering function", func.__name__) + result = func(*args, **kwargs) + print(f"{log_level}: Exiting function", func.__name__) + return result + return wrapper + return log_function # return the new function + +@log_function_with_args(log_level="INFO") # Now you can set arguments +def my_function(x,y): + """Adds two numbers together.""" + return x+y + +my_function(1,2) # => "INFO: Entering function my_function" + # => "3" + # => "INFO: Exiting function my_function" ``` ### Free Online