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

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]):

Tags:

Python