python - 有没有更好的方法可以在不调用两次的情况下恢复 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)):
但我有一个语法错误
解决方案
调用函数时不必解包。
将结果存储在变量中然后测试
result = teste(x)
if result:
a,b,c = result
else:
print("false")
推荐阅读
- php - 在特定页面执行功能
- swift - 比较从两个字符串计算的值
- python - PiCamera - 如何将“rgb”格式的 np.array 捕获保存到文件中?
- sql - PostgreSQL 随机子查询
- javascript - 我应该使用哪个功能?.toDataURL() 还是 .toBlob()?
- android - Android VideoView 不会在树莓派上播放视频
- google-apps-script - Ebay API:ActiveInventoryReport 数据
- typo3 - 如何在文件夹中隐藏后端布局
- failovercluster - 将 Hypervisor 添加回故障转移集群
- ios - 如何使 Xcode segues 不可逆?