checking if two things do not equal None

Roy Smith roy at panix.com
Sat Mar 29 18:51:43 EDT 2014


In article <mailman.8703.1396133206.18130.python-list at python.org>,
 Tim Chase <tim at thechases.com> wrote:

> On 2014-03-29 18:41, Roy Smith wrote:
> > On Mar 29, 2014, at 6:36 PM, Tim Chase wrote:
> > 
> > > 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()
> > 
> > I might have written that as:
> > 
> > if set([a, b, c, d]) == set(None)
> > 
> > That's even clearer if you happen to already have the items in an
> > iterable:
> > 
> > if set(conditions) == set(None)
> 
> Though am I correct that your iteration tests for equality, while
> mine tests for identity?

Hmmm, you're almost certainly correct on that, but you would have to 
have a perversely designed class for that to make a difference.  I'll 
take the increased readability.

> Also, my version bails early in the event
> quitting early is possible.  That's particularly useful in the case
> of doing something like
> 
>   if all(x() is None for x in [func1, func2, func3, costly_func]):
>     do_something()

Again, you're correct.  But, I'll take the increased readability over 
the premature optimization :-)



More information about the Python-list mailing list