Skip to content

Instantly share code, notes, and snippets.

@raymondng76
Created August 9, 2020 13:08
Show Gist options
  • Save raymondng76/df80b60f3fd77f9b18c97315665d200b to your computer and use it in GitHub Desktop.
Save raymondng76/df80b60f3fd77f9b18c97315665d200b to your computer and use it in GitHub Desktop.
Decorators Example #python #decorator
# Decorators using the @ symbol ##########
def double_args(func):
def wrapper(a, b):
return func(a * 2, b * 2) # Modify input arguments before passing to requested function
return wrapper
@double_args # This is to indicate the decorators is used on the multiple function
def multiple(a, b):
return a * b
if __name__ == '__main__':
m = multiple(1, 5) # No need to call double_args to wrap the multiple method as the @ declaration already done that
print(m)
##########
# 20 (Answers is 20 because warpper * 2 al arguements before calling the multply method)
##########
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment