在 Python 3 中访问装饰器内部装饰函数的关键字参数失败

Accessing keyword argument of decorated function inside the decorator fails in Python 3

kwargs 在以下代码中为空。 如何访问修饰函数的超时关键字arg?

import functools
def retriable(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        timeout = kwargs['timeout']
        criteria_satisfied = func(*args, **kwargs)
        while not criteria_satisfied and timeout > 0:
            time.sleep(5)
            timeout -= 5
            criteria_satisfied = func(*args, **kwargs)
        return criteria_satisfied
    return wrapper

@retriable
def decorated(ip, timeout=60):
    ... some logic
    return True 

decorated(ip)

解决方案是制作带参数的装饰器。这将 return 函数上的另一个装饰器,并将使用参数值填充创建的闭包。

import functools
def retriable_with_arg(timeout):
    def retriable(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            alive_timeout = timeout
            criteria_satisfied = func(*args, **kwargs)
            while not criteria_satisfied and alive_timeout > 0:
                time.sleep(5)
                alive_timeout -= 5
                criteria_satisfied = func(*args, **kwargs)
            return criteria_satisfied
     return wrapper
return retriable

@retriable(timeout=60)
def decorated(ip, timeout=60):
    ... some logic
    return True 

decorated(ip)