Python 返回多个值并检查是否返回 False

标签 python python-3.x

我看到很多关于如何在函数中返回多个值的好建议,但是处理检查其他返回值(如 False)的首选方法是什么?

例如:

def f():
    if condition1:
        return False
    else:
        return x, y, z

x, y, z = f()

我可以验证 if [x, y, z] 不是 None: 但是是否也检查 False 呢?只是 if [x, y, z] is not None and f() is not False: 还是有更好的方法?

最佳答案

我认为提高一致性会有所帮助:

def f():
    if condition1:
        return False, None
    else:
        return True, (x, y, z)

success, tup = f()
if success:
    x, y, z = tup
    # use x, y, z...

关于Python 返回多个值并检查是否返回 False,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40953914/

相关文章:

python - pygame.mixer.music.play() 无法识别 Fast Tracker(.xm 音乐格式)重复位置

python/matplotlib 绘图相当于 R 的符号图?

python-3.x - 从 Python 2 移植到 Python 3 : 'utf-8 codec can' t decode byte'

python - 检查键是否存在并使用 Python 迭代 JSON 数组

用于连接的 Python 运算符重载

python - 如何修复 Selenium 不清除输入上的默认文本

python-3.x - 如何从 python 和我的系统以及命令提示符中删除 nltk

python - 如何使用 python-click 构建类似 kubectl 的 CLI?

python - 在 Jupyter 中使用 @interact 装饰器时实现 "reset"按钮

python - GridLayout 中的小部件相互重叠