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

If I have a design 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 this how I wrote, or another better way?

This is different from this question: not tested by any test in Python ... I am looking for a preferred test method if many conditions are nothing. The option I typed seems very long and non-neat.

+7
python
source share
1 answer

There is nothing wrong with how you do it.

If you have many variables, you can put them in a list and use all :

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

All Articles