Created
January 3, 2018 01:41
-
-
Save SegFaultAX/47123c75bba349dfe463e63985615ae7 to your computer and use it in GitHub Desktop.
Decorator Example [Python]
This file contains hidden or 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
def log_arguments(function_to_wrap): | |
def new_wrapped_function(*args, **kwargs): | |
name = function_to_wrap.func_name | |
print("Calling {} with args {} and kwargs {}".format( | |
name, args, kwargs)) | |
return function_to_wrap(*args, **kwargs) | |
return new_wrapped_function | |
def add(a, b): | |
return a + b | |
add_with_logging = log_arguments(add) | |
print(add(1, 2)) | |
add_with_logging(3, 4) | |
@log_arguments | |
def add2(a, b): | |
return a + b | |
add2(5, 6) | |
### Output: | |
# 3 | |
# Calling add with args (3, 4) and kwargs {} | |
# Calling add2 with args (5, 6) and kwargs {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment