Equivalent code to the bool() built-in function
Duncan Booth
duncan.booth at invalid.invalid
Thu Apr 28 13:18:36 EDT 2011
Albert van der Horst <albert at spenarnc.xs4all.nl> wrote:
>>I guess I never thought about it, but there isn't an 'xor' operator to
>>go along with 'or' and 'and'. Must not be something I need very often.
>
> There is. <> applied to booleans is xor.
>
Best to get into the habit of using '!=' otherwise you'll find Python 3.x a
bit frustrating.
However, that only works if you have exactly two values: both 'and' and
'or' will extend easily to any number of values:
>>> True and False and True
False
>>> False or False or True or False
True
but using != for 'xor' doesn't extend the same way:
>>> False != False != True
False
You can use 'sum(...)==1' for a larger number of values but do have to be
careful that all of them are bools (or 0|1).
--
Duncan Booth http://kupuguy.blogspot.com
More information about the Python-list
mailing list