Posts

Showing posts with the label decorator

How to pass parameters to the innermost function when i use python decorator with parameters? [duplicate]

How to pass parameters to the innermost function when i use python decorator with parameters? [duplicate] This question already has an answer here: here is my decorator for recall func when func return is not True def deco_retry(retry_times): def _deco_retry(func): def wrapper(*args, **kwargs): while retry_times > 0: ret = func(*args, **kwargs) if ret: return ret retry_times -= 1 return wrapper return _deco_retry @deco_retry(retry_times=1) def func(ok=1): if ok == 1: return True else: return False when i call func(), error occurred: Traceback (most recent call last): File "E:/Charles/Code/pycharmprj/Huobi/test_code/decorator_test.py", line 26, in <module> func() File "E:/Charles/Code/pycharmprj/Huobi/test_code/decorator_test.py", line 10, in wrapper while retry_times > 0: UnboundLocalError: local variable 'retry_...