there is another to implement decorator using class and you can also pass arguments to the decorator itself
here is an example of a logger helper decorator, which you can pass the function scope and the returned value when it fails
import logging
class LoggerHelper(object):
def __init__(self, scope, ret=False):
self.scope = scope
self.ret = ret
def __call__(self, original_function):
def inner_func(*args, **kwargs):
try:
logging.info(f"*** {self.scope} {original_function.__name__} Excuting ***")
return original_function(*args, **kwargs)
logging.info(f"*** {self.scope} {original_function.__name__} Executed Successfully ***")
except Exception as e:
logging.error(f"*** {self.scope} {original_function.__name__} Error: {str(e)} ***")
return self.ret
return inner_func
and when you use it, you can easily track where the exception was raised
class Example:
@LoggerHelper("Example", ret=False)
def method:
print(success)
return True
test2, you're callingdecAny('xxx'). ButdecAnytakes a function,f0, not a string. So clearly at some point, thatf0()is going to try to call'xxx'.@decAny, that's just usingdecAnyitself as a decorator. But if you have@decAny(), that's callingdecAnybefore you even get to decorating, just as@decAny('xxx')is. (It's just like when you pass functions as values, store them in variables, etc., as opposed to calling them.)