简体   繁体   中英

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

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?

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.

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

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.

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 :

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

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.

$ python3 -m timeit "all(v is not None for v in [1, 84, 'String', (6, 'Tuple'), 3])"
200000 loops, best of 5: 999 nsec per loop

$ python3 -m timeit "None not in [1, 84, 'String', (6, 'Tuple'), 3]"
2000000 loops, best of 5: 184 nsec per loop

$ python3 -m timeit "None not in (1, 84, 'String', (6, 'Tuple'), 3)"
2000000 loops, best of 5: 184 nsec per loop

python3 -m timeit "None not in {1, 84, 'String', (6, 'Tuple'), 3}"
5000000 loops, best of 5: 48.6 nsec per loop

Another advantage of this method is that it gives you the correct answer even if someone defines the __eq__ method of a class to always return True . (Of course, if they define the __hash__ method to return hash(None) , this method won't work. But nobody should do that, because it would defeat the purpose of defining a hash.)

class my_int(int):
    def __init__(self, parent):
        super().__init__()

    def __eq__(self, other):
        return True

    def __hash__(self):
        return hash(super())

print(all(v is not None for v in [1, my_int(6), 2])) # True (correct)
print(None not in [1, my_int(6), 2])                 # False (wrong)
print(None not in (1, my_int(6), 2))                 # False (wrong)
print(None not in {1, my_int(6), 2})                 # True (correct)

For the specific case presented by the OP

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

will be enough.

all([a, b, c])

evaluates to False if any parameter is missing.

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:

>>> all(v is not None for v in [False])
True
>>> all([False])
False
x = 'a'
y = 'b'
z = 'c'

if all(item is not None for item in [x, y, x]):
    print('Multiple variables are NOT None')
else:
    print('At least one of the variables stores a None value')

Complementing Daniel Roseman's answer, I think:

if all([a,b,c,d]):

is cleaner

Since all() built-in function already iterates over the list checking for None values

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM