Comparison with False - something I don't understand
Harishankar
v.harishankar at gmail.com
Thu Dec 2 05:02:03 EST 2010
On Thu, 02 Dec 2010 00:15:42 -0800, Alice Bevan–McGregor wrote:
> Howdy!
Good day to you!
> (False == 0) is True
> (True == 1) is True
I see. Thanks for this. I suspected this, but wasn't sure.
> The bool type is a subclass of int! (Run those lines in a Python
> interpreter to see. ;)
>
>> if var == False:
>
> if var is False: …
So "var is False" is safer to use when I want to specifically check
whether var is set to False and not 0 or None?
> If you want to check not just for value equivelance (False == 0) but
> literal type, use the "is" comparator. "is" checks, and others correct
> me if I'm wrong, the literal memory address of an object against
> another. E.g. False, being a singleton, will always have the same
> memory address. (This is true of CPython, possibly not of Python
> implementations like Jython or IronPython.) Using "is" will be
> effective for checking for literal None as well.
Thanks, it makes sense to me now. Literal equivalence is what I was
looking for. I didn't quite understand whether == achieved this or not.
Now I guess I know.
>
> When ever I need to test for None, I always use the "is" comparator.
> It's also more English-like. (None, evaluating to False when using
> '==', is useful when all you care about is having a blank default value,
> for example.)
Yes, but in my function I don't want to confuse False with 0 or anything
else except False. Thanks again for explaining this clearly.
--
Harishankar (http://harishankar.org http://lawstudentscommunity.com)
More information about the Python-list
mailing list