Accessing Self Inside Decorator with parameters
Brian Herman
brianherman at gmail.com
Thu Dec 14 14:44:44 EST 2017
I would like to access the instance variable of a class using a decorator
with arguments:
I used to do with with a decorator without arguments:
def decorator(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
class test1:
def call_this(self):
print("this works")
@decorator
def preform_action(self):
print("test")
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
class test2:
def call_this(self):
print("this does not")
@decorator_with_arguments(1,2)
def preform_action(self):
print("test")
Is there any way to do this pythonically?
--
Thanks,
Brian Herman
kompile.org <http://www.kompile.org>
More information about the Python-list
mailing list