Created
May 7, 2020 16:41
-
-
Save jonathan-s/c9a644992b0deb90dba891c7cf8ae555 to your computer and use it in GitHub Desktop.
Reminder for how decorators work, so I don't need to look it up.
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
def method_decorator(argument, argument2): | |
# decorator name used here. | |
def decorator(func, argument=argument, argument2=argument2): | |
# the function passed and we need to pass the arguments as well. | |
# I couldn't get it to work otherwise. | |
def wrapped(self, argument=argument, argument2=argument2, *fargs, **fkwargs): | |
# the first argument is self, this is a decorator for methods. | |
# passing the arguments again. Otherwise trouble. | |
# here we also get the function arguments. | |
# do something with arguments that you had. | |
# call the original function | |
result = func(self, *fargs, **fkwargs) | |
# return the result. | |
return result | |
return wrapped | |
return decorator | |
class MyClass: | |
@method_decorator('hello', 'world') | |
def method(self): | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment