Python调用外部函数的返回与内部函数的返回,而不检查内部函数的返回值

问题描述 投票:-1回答:1

考虑以下装饰器:

def wraper(function):
    @wraps(function)
    def wrap(*args,**kwargs): 
        return function(*args, **kwargs)

装饰功能:

@wraper
def function():
      return 1

考虑其他功能:

def function2():
    print("In function")
    variable = function()   #Return without assigning the variable, from function2, with the return of function 
    print("Returning from function2")
    return variable

我希望function2的输出为:

在功能中

不要这样:

在功能中

从功能2返回

python python-3.x function return python-decorators
1个回答
0
投票

variable = function()将始终在python中分配变量,除非它在内部引发错误。如果函数不返回任何内容variable = None。您的验证器没有任何作用,因为它只是返回具有相同参数的原始函数,所以它等于根本没有任何目的。我可以帮助您实现所需的目标,但您必须明确您的意图。

© www.soinside.com 2019 - 2024. All rights reserved.