【问题标题】:python decorator TypeError missing 1 required positional argumentpython 装饰器 TypeError 缺少 1 个必需的位置参数
【发布时间】:2019-01-24 07:02:38
【问题描述】:

我正在尝试编写一个装饰器来重复错误函数 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()

我不断收到错误:

TypeError: our_decorator() missing 1 required positional argument: 'func'

【问题讨论】:

    标签: python python-decorators


    【解决方案1】:

    你应该使用:

    @exponential_backoff()
    def test():
        ...
    

    整体装饰器没有设计成参数是可选的,所以使用时必须提供()

    如果想要一个如何使装饰器允许参数列表成为可选的示例,请参阅:

    您也可以考虑使用 wrapt 包来使您的装饰器更简单、更健壮。

    【讨论】:

    • 我明白了。太棒了!
    【解决方案2】:

    您可以选择@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()
    

    编辑 我的回答不是不完全正确,请参阅@GrahamDumpleton 的回答,它展示了如何使我的解决方案尝试可行(即this link)。现在修复它,谢谢@GrahamDumpleton!

    【讨论】:

    • 我想您会发现这将不允许您使用装饰器语法提供 secondsattempts 参数。您需要一个特殊技巧,如我在答案中链接的文档中所示。
    • 这是真的@GrahamDumpleton!请接受我的解决方案不如你的解决方案……我还在学习!支持@GrahamDumpleton 解决方案的其他资源:python-3-patterns-idioms-test.readthedocs.io/en/latest/…
    【解决方案3】:

    了解什么是装饰器:

    @exponential_backoff
    def test():
        pass
    

    等于:

    def test():
        pass
    
    test = exponential_backoff(test)
    

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


    那么进一步:

    @exponential_backoff()
    def test():
        pass
    

    等于:

    def test():
        pass
    
    test = exponential_backoff()(test)
    

    在这种情况下,现在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>
    

    【讨论】:

      猜你喜欢
      • 2019-09-07
      • 1970-01-01
      • 2013-07-06
      • 2019-06-25
      • 2013-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多