【发布时间】:2017-02-21 07:20:02
【问题描述】:
如果我有这样的构造:
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..."
python 中检查所有变量是否都设置为有用值的首选语法是什么?是像我写的那样,还是其他更好的方法?
这与这个问题不同: not None test in Python ...我正在寻找检查许多条件是否不是无的首选方法。我输入的选项看起来很长而且不是pythonic。
【问题讨论】:
-
我正在检查四个变量,并且认为您发布的明显解决方案不是很pythonic。我很遗憾不得不从缺乏答案中得出结论,似乎没有“更好”的方式。
-
@schwobaseggl:不,这不是重复的,因为问题不在于如何检查无。它是关于如何在检查多个变量时使其“看起来更 Pythonic”,但没有那么多变量来纠正 Daniel Roseman 提出的解决方案。
-
def all_is_not_None(*args): return all(x is not None for x in args)
标签: python