If you just want to check for a falsy value:
myval = findit('something')
if not myval:
"not found"
You don't want to do this if you could actually return a value for the "is-found" case which has a falsy value. E.g., if []
(empty list) is found, this will still report "not found".
If you care that it is actually False
, use is
. True
, False
, and None
are singletons, so the identity comparison operator is used.
if myval is False:
"not found"
In this case you are using False
as a sentinel value to indicate that there is no result.
Usually None
is used as a sentinel "no-value", however, so I suggest you change the signature of your function to return None
if no value is found. This conforms better with Python idiom and the behavior of methods such as {}.get('missing-key')
.
myval = findit('something')
if myval is None:
"not found"
In cases where no sentinel value is possible because any value could be returned (even None
or False
), Python uses an exception to signal that no value was found. E.g. {}['missing-key']
will raise KeyError
.