Created
March 14, 2022 23:06
-
-
Save CEZERT/dc87c062ed2a95a7f8482a1205c452ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import functools | |
def debug(func): | |
"""Print the function signature and return value""" | |
@functools.wraps(func) | |
def wrapper_debug(*args, **kwargs): | |
args_repr = [repr(a) for a in args] # 1 | |
kwargs_repr = [f"{k}={v!r}" for k, v in kwargs.items()] # 2 | |
signature = ", ".join(args_repr + kwargs_repr) # 3 | |
print(f"Calling {func.__name__}({signature})") | |
value = func(*args, **kwargs) | |
print(f"{func.__name__!r} returned {value!r}") # 4 | |
return value | |
return wrapper_debug | |
@debug | |
def make_greeting(name, age=None): | |
if age is None: | |
return f"Howdy {name}!" | |
else: | |
return f"Whoa {name}! {age} already, you are growing up!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment