On Thu, 02 Dec 2010 07:28:30 +0000, Harishankar wrote:
> When I run pychecker through my modules I get the message that
> comparisons with "False" is not necessary and that it might yield
> unexpected results.
>
> Yet in some situations I need to specifically check whether False was
> returned or None was returned. Why is comparison with False so bad?
The behaviour may be counterintuitive.
One might expect that "x == False" is equivalent to "not x". Sometimes it
is, sometimes it isn't.
E.g. 0 and 0.0 are equal to False and are equivalent to False when
converted to booleans:
> 0 == False
True
> not 0
True
> 0.0 == False
True
> not 0.0
True
[], "" and None aren't equal to False but are equivalent to False when
converted to booleans:
> [] == False
False
> not []
True
> "" == False
False
> not ""
True
> None == False
False
> not None
True
The boolean conversions are what's relevant for "if x ...", "while x ...",
etc.
If you want to test specifically for True, False or None, use "is" rather
than an equality check. This eliminates the warning and doesn't
risk misleading someone reading the code.
--
http://mail.python.org/mailman/listinfo/python-list