Skip to content

Instantly share code, notes, and snippets.

@SegFaultAX
Created January 3, 2018 01:41
Show Gist options
  • Save SegFaultAX/47123c75bba349dfe463e63985615ae7 to your computer and use it in GitHub Desktop.
Save SegFaultAX/47123c75bba349dfe463e63985615ae7 to your computer and use it in GitHub Desktop.
Decorator Example [Python]
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