If I have a construct like this:
JavaScript
x
12
12
1
def foo():
2
a=None
3
b=None
4
c=None
5
6
#...loop over a config file or command line options...
7
8
if a is not None and b is not None and c is not None:
9
doSomething(a,b,c)
10
else:
11
print "A config parameter is missing..."
12
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?
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.
Advertisement
Answer
There’s nothing wrong with the way you’re doing it.
If you have a lot of variables, you could put them in a list and use all
:
JavaScript
1
2
1
if all(v is not None for v in [A, B, C, D, E]):
2