checking if two things do not equal None

Tim Chase python.list at tim.thechases.com
Sat Mar 29 23:36:55 CET 2014


On 2014-03-29 17:07, Roy Smith wrote:
> > if (a is not None) or (b is not None):
> > 
> > is immediately understandable by everyone?  
> 
> I agree with that.  But
> 
> > if (a, b) != (None, None):  
> 
> seems pretty straight-forward to me too.  In fact, if anything, it
> seems easier to understand than

And for cases where you have more than one or two things to test for
None-itude, you could use

  if all(x is None for x in [a, b, c, d]):
    do_something_if_theyre_all_None()

or

  if all(x is not None for x in [a, b, c, d]):
    do_something_if_no_Nones()

or

  if not any(x is None for x in [a, b, c, d]):
    do_something_if_no_Nones()

which I find *much* more readable from a maintenance point of view.

-tkc






More information about the Python-list mailing list