Created
August 9, 2020 13:08
-
-
Save raymondng76/df80b60f3fd77f9b18c97315665d200b to your computer and use it in GitHub Desktop.
Decorators Example #python #decorator
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
# 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