装饰外部(库)函数的正确方法是什么?

Fla*_*lau 1 python decorator python-2.7 python-decorators

我在我的代码中多次使用库函数来测试通过/失败条件并相应地执行不同的代码,但由于某种原因,它找不到结果的返回值; 我想用装饰器添加它,以便我可以在我的代码中调用它.

鉴于我无法编辑源文件,这样做的正确方法是什么?

我应该这样做:

def test_pass(param1, param2):
    external_function(param1, param2)
    if(...):
        return False
    else:
        return True
Run Code Online (Sandbox Code Playgroud)

或者有没有办法使用漂亮的@decorator语法?

Mar*_*ers 5

装饰用的@decorator语法糖 ; 函数对象被decorator(orig_function)调用返回的任何内容替换.

对于外部函数,您只需使用您编写的包装器; 在定义原始函数时,您只使用装饰器语法.