相关疑难解决方法(0)

python decorator TypeError缺少1个必需的位置参数

我正在尝试编写一个装饰器以重复N次错误函数,并且之间的睡眠时间不断增加。到目前为止,这是我的尝试:

def exponential_backoff(seconds=10, attempts=10):
    def our_decorator(func):
        def function_wrapper(*args, **kwargs):
            for s in range(0, seconds*attempts, attempts):
                sleep(s)
                try:
                    return func(*args, **kwargs)
                except Exception as e:
                    print(e)
        return function_wrapper
    return our_decorator

@exponential_backoff
def test():    
    for a in range(100):
        if a - random.randint(0,1) == 0:
            print('success count: {}'.format(a))
            pass
        else:
            print('error count {}'.format(a))
            'a' + 1

test()
Run Code Online (Sandbox Code Playgroud)

我不断收到错误:

TypeError: our_decorator() missing 1 required positional argument: 'func'
Run Code Online (Sandbox Code Playgroud)

python python-decorators

4
推荐指数
3
解决办法
3358
查看次数

标签 统计

python ×1

python-decorators ×1