I found this kind of expression several times in a python program:
if variable is not None:
dothings(variable)
It seems strange to me, and I think that it has no more sense than:
if variable:
dothings(variable)
Maybe I don’t know Python enough, and the expression is explained somewhere?
variablecould be0, orFalse, or[], or(); be ‘falsy’ in other words, and then theifstatement would be skipped.See Truth testing for more detail on what is considered false in a boolean context.
In short, testing if
variable is not Noneallowsvariableto be anything else, including values that would otherwise be considered False in a boolean context.