missing 'xor' Boolean operator
Bill Davy
Bill at SynectixLtd.com
Wed Jul 15 10:37:54 EDT 2009
"MRAB" <python at mrabarnett.plus.com> wrote in message
news:mailman.3158.1247667680.8015.python-list at python.org...
> Steven D'Aprano wrote:
>> On Tue, 14 Jul 2009 11:25:08 -0700, Dr. Phillip M. Feldman wrote:
>>
>>> Current Boolean operators are 'and', 'or', and 'not'. It would be nice
>>> to have an 'xor' operator as well.
>>
>> I've often wished there was too, for the sake of completeness and
>> aesthetics, I'd love to be able to write:
>>
>> a xor b
>>
>> instead of defining a function xor(a, b).
>>
>> Unfortunately, outside of boolean algebra and simulating electrical
>> circuits, I can't think of any use-cases for an xor operator. Do you have
>> any?
>>
> The problem is that 'and' and 'or' are not limited to Boolean values:
>
> 'and' returns the first false value or the last true value.
>
> 'or' returns the first true value or the last false value.
>
> What values should 'xor' return? IMHO, if only one of the values is true
> then it should return that value, otherwise it should return False.
>
> 1 xor 0 => 1
> 0 xor 2 => 2
> 1 xor 2 => False
> 0 xor 0 => False
>
> This is because it's a Boolean operator, so it should fall back to
> Boolean values when necessary, like 'not':
>
> not 0 => True
> not 1 => False
>
> Also:
>
> x and y and z => (x and y) and z
> x or y or z => (x or y) or z
>
> therefore:
>
> x xor y xor z => (x xor y) xor z
Gosh, let's all discuss commutation and distribution.
And surely in quantum merchanics there is something about non-commuting
operatiomns letting in Planck's constant.
More information about the Python-list
mailing list