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

Blu*_*oon 4 python python-decorators

我正在尝试编写一个装饰器以重复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)

Sra*_*raw 10

了解什么是装饰器:

@exponential_backoff
def test():
    pass
Run Code Online (Sandbox Code Playgroud)

等于:

def test():
    pass

test = exponential_backoff(test)
Run Code Online (Sandbox Code Playgroud)

在这种情况下,testdef our_decorator(func):。这就是为什么你TypeError在调用test().


更进一步:

@exponential_backoff()
def test():
    pass
Run Code Online (Sandbox Code Playgroud)

等于:

def test():
    pass

test = exponential_backoff()(test)
Run Code Online (Sandbox Code Playgroud)

在这种情况下,现在test就是您所需要的。


此外,functools.wraps帮助您将原始函数的所有属性复制到修饰函数。例如函数名或文档字符串:

from functools import wraps

def exponential_backoff(func):
#   @wraps(func)
    def function_wrapper(*args, **kwargs):
        pass
    return function_wrapper

@exponential_backoff
def test():
    pass

print(test)  # <function exponential_backoff.<locals>.function_wrapper at 0x7fcc343a4268>
# uncomment `@wraps(func)` line:
print(test)  # <function test at 0x7fcc343a4400>
Run Code Online (Sandbox Code Playgroud)


Gra*_*ton 7

您应该使用:

@exponential_backoff()
def test():
    ...
Run Code Online (Sandbox Code Playgroud)

整体装饰器的设计并非使参数为可选,因此()在使用时必须提供。

如果要使装饰器允许参数列表成为可选的示例,请参见:

您可能还考虑使用wrapt包使装饰器更轻松,更坚固。


Tho*_*sse 5

您可以选择@Graham Dumpleton 提供的解决方案,也可以像这样修改您的装饰器:

from functools import wraps, partial

def exponential_backoff(func=None, seconds=10, attempts=10):
    if func is None:
        return partial(exponential_backoff, seconds=seconds, attempts=attempts)

    @wraps(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

@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)

编辑 我的答案并不完全正确,请参阅@GrahamDumpleton的答案,它展示了如何使我的解决方案尝试可行(即此链接)。立即修复,谢谢@GrahamDumpleton!

  • 我想你会发现这不允许你使用装饰器语法提供“秒”和“尝试”参数。您需要一个特殊的技巧,如我在答案中链接的文档所示。 (3认同)