Created
May 7, 2020 16:41
Revisions
-
jonathan-s created this gist
May 7, 2020 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ 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