Skip to content

Instantly share code, notes, and snippets.

@jonathan-s
Created May 7, 2020 16:41

Revisions

  1. jonathan-s created this gist May 7, 2020.
    26 changes: 26 additions & 0 deletions decorator.py
    Original 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