检查多个变量是否不是 None 的最 Pythonic 方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42360956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:36:56  来源:igfitidea点击:

What is the most pythonic way to check if multiple variables are not None?

python

提问by Eyelash

If I have a construct like this:

如果我有这样的构造:

def foo():
    a=None
    b=None
    c=None

    #...loop over a config file or command line options...

    if a is not None and b is not None and c is not None:
        doSomething(a,b,c)
    else:
        print "A config parameter is missing..."

What is the preferred syntax in python to check if all variables are set to useful values? Is it as I have written, or another better way?

python中检查所有变量是否设置为有用值的首选语法是什么?是像我写的那样,还是其他更好的方法?

This is different from this question: not None test in Python... I am looking for the preferred method for checking if many conditions are not None. The option I have typed seems very long and non-pythonic.

这与这个问题不同: not None test in Python...我正在寻找检查许多条件是否不是 None 的首选方法。我输入的选项似乎很长且非 Pythonic。

回答by serge.v

It can be done much simpler, really

它可以做得更简单,真的

if None not in (a, b, c, d):
    pass

UPDATE:

更新:

As slashCoder has correctly remarked, the code above implicitly does a == None, b == None, etc. This practice is frowned upon. The equality operator can be overloaded and not None can become equal to None. You may say that it never happens. Well it does not, until it does. So, to be on the safe side, if you want to check that none of the objects are None you may use this approach

正如 slashCoder 正确评论的那样,上面的代码隐含地做了 a == None、b == None 等。这种做法是不受欢迎的。相等运算符可以重载,而不是 None 可以变为等于 None。你可能会说它永远不会发生。好吧,它不会,直到它发生。所以,为了安全起见,如果你想检查没有一个对象是 None 你可以使用这种方法

if not [x for x in (a, b, c, d) if x is None]:
    pass

It is a bit slower and less expressive, but it is still rather fast and short.

它有点慢且表现力较差,但它仍然相当快和短。

回答by Daniel Roseman

There's nothing wrong with the way you're doing it.

你这样做的方式没有任何问题。

If you have a lotof variables, you could put them in a list and use all:

如果你有很多变量,你可以把它们放在一个列表中并使用all

if all(v is not None for v in [A, B, C, D, E]):

回答by Kamaraju Kusumanchi

Writing a separate answer as I do not know how to format code when added as a comment.

写一个单独的答案,因为我不知道在作为评论添加时如何格式化代码。

Eternal_N00B's solution is not same as Daniel Roseman's solution. Consider for example:

Eternal_N00B 的解决方案与 Daniel Roseman 的解决方案不同。考虑例如:

>>> all(v is not None for v in [False])
True
>>> all([False])
False

回答by tfpf

I know this is an old question, but I wanted to add an answer which I believe is better.

我知道这是一个老问题,但我想添加一个我认为更好的答案。

If all elements which have to be checked are hashable, you could use a set instead of a list or tuple.

如果必须检查的所有元素都是可散列的,则可以使用集合而不是列表或元组。

>>> None not in {1, 84, 'String', (6, 'Tuple'), 3}

This is much faster than the methods in the other answers.

这比其他答案中的方法快得多。

>>> import timeit
>>> timeit.timeit("all(v is not None for v in [1, 84, 'String', (6, 'Tuple'), 3])")
1.7880705000000034
>>> timeit.timeit("None not in [1, 84, 'String', (6, 'Tuple'), 3]")
0.35424169999998867
>>> timeit.timeit("None not in (1, 84, 'String', (6, 'Tuple'), 3)")
0.3454340999999772
>>> timeit.timeit("None not in {1, 84, 'String', (6, 'Tuple'), 3}")
0.09577370000002361

回答by papok

For the specific case presented by the OP

对于 OP 提出的特定案例

if not all([a, b, c])

will be enough.

就足够了。

all([a, b, c])

evaluates to False if any parameter is missing.

如果缺少任何参数,则计算结果为 False。