comparing booleans
Dang Griffith
noemail at noemail4u.com
Thu Jan 29 08:10:23 EST 2004
On Wed, 28 Jan 2004 16:17:22 -0800, Erik Max Francis <max at alcyone.com>
wrote:
>Gerrit Holl wrote:
>
>> is it proper to compare booleans? It is possible, of course, because
>> they're compatible with numbers, but booleans aren't truly numbers.
>> I'm
>> tempted to write:
>>
>> return cmp(self.extends, other.extends)
>
>Even if you're seriously worried about the correctness of comparing
>Booleans, you can always explicitly turn them into integers:
>
> return cmp(int(self.extends), int(other.extends))
>
>> (Hmm, makes me wonder, for booleans, are != and ^ equal?)
>
>Easily checked:
>
>>>> for x in (True, False):
>... for y in (True, False):
>... print x, y, x != y, x ^ y
>...
>True True False False
>True False True True
>False True True True
>False False False False
It's not always the same, as shown here:
>>> True != True != True
False
>>> True ^ True ^ True
True
>>> True != False != True != True
False
>>> True ^ False ^ True ^ True
True
Not that I've never used this (cool) Python syntax in practice,
but I thought it was worth mentioning that using != and ^ in
boolean expressions does not always give the same result. It does
give the same result when there are only two operands.
Interestingly, and I'm not sure why:
>>> (True != True) != True
True
>>> True != (True != True)
True
>>> True != True != True
False
--dang
More information about the Python-list
mailing list