Well, no, you wouldn't want to do that. if not a is the accepted shorthand for if a == False, but False and None are distinct values. You should always explicitly test for None, although you can implicitly test for "not None":
iffoo:# foo has value (not None) OR foo is True
ifnotfoo:# foo is False
iffooisNone:# foo is None
There's actually a bit more to it than that. if not checks "truthyness", not just equality to False. So not foo will evaluate to True if foo is any of these and more:
False
None
0
[]
{}
""
The same thing applies in the other direction, if [1] will succeed because the list is not empty, empty list are "falsey" whereas lists containing elements are "truthy".
The lesson here is pretty much don't use if without an explicit comparison (== or is) unless you're sure you know how truthy the value will be in all cases.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Well, no, you wouldn't want to do that.
if not a
is the accepted shorthand forif a == False
, butFalse
andNone
are distinct values. You should always explicitly test forNone
, although you can implicitly test for "not None":Awesome.
There's actually a bit more to it than that.
if not
checks "truthyness", not just equality to False. Sonot foo
will evaluate toTrue
iffoo
is any of these and more:False
None
0
[]
{}
""
The same thing applies in the other direction,
if [1]
will succeed because the list is not empty, empty list are "falsey" whereas lists containing elements are "truthy".The lesson here is pretty much don't use
if
without an explicit comparison (==
oris
) unless you're sure you know how truthy the value will be in all cases.