Accessing Self Inside Decorator with parameters

Chris Angelico rosuav at gmail.com
Thu Dec 14 16:03:38 EST 2017


On Fri, Dec 15, 2017 at 6:44 AM, Brian Herman <brianherman at gmail.com> wrote:
> However when I add the arguments decorator I cannot access the self of the
> object that calls the decorator.
>
> def decorator_with_arguments(arg1, arg2):
>     def decorator(self, method):
>         """ This method is a decorator to get a security token each time
> you have a service call. """
>         def wrapper(self, *args, **kw):
>             self.call_this()
>             result = method(self, *args, **kw)
>
>             return result
>         return wrapper

Why does the decorator take a self parameter? The wrapper function
does, but when your decorator() runs, it's just looking at a function
and returning a function. Try removing self from that and see if it
solves your problem.

ChrisA



More information about the Python-list mailing list