首页 > 解决方案 > 有没有更好的方法可以在不调用两次的情况下恢复 IF 内的 func 参数?

问题描述

我有一段代码,它正在工作,但是执行需要很长时间,我制作了这个简化版本来说明我的问题

def teste(x):
    if x > 1:
        return x, "whatever", {'foo':'bar'}
    else:
        return False

x = 2

if teste(x):
    a,b,c = teste(x)
else:
    print("false")

有更好的方法吗?我努力了

if(a,b,c = teste(x)):

但我有一个语法错误

标签: pythonfunctionif-statement

解决方案


调用函数时不必解包。

将结果存储在变量中然后测试

result = teste(x)
if result:
    a,b,c = result
else:
    print("false")

推荐阅读