我有两个装饰器,@timeout和@retry
代码是这样的

@timeout(seconds=1)
def func_inner(expire):
    time.sleep(expire)

@retry(
    count=2,
    message="Failed command after {expire} seconds",
)
def func(expire):
    func_inner(expire)

我只想知道func()方法如何知道func_inner有一个decorator@timeout提前谢谢!

最佳答案

此代码:

@timeout(seconds=1)
def func_inner(expire):
    time.sleep(expire)

基本上等于:
def func_inner(expire):
    time.sleep(expire)

func_inner = timeout(seconds=1)(func_inner)

方法func只调用func_inner(expire),这与调用timeout(seconds=1)(func_inner)(expire)相同,因为decorator重新定义了函数。

10-06 06:30