missing 'xor' Boolean operator
Ethan Furman
ethan at stoneleaf.us
Mon Jul 20 18:34:28 EDT 2009
[fixed for bottom-posting]
Dr. Phillip M. Feldman wrote:
> MRAB-2 wrote:
>
>>
>><snip>
>>
>>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
>>
> Suppose that 'xor' returns the value that is true when only one value is
> true, and False otherwise. This definition of xor doesn't have the
standard
> associative property, that is,
>
> (a xor b) xor c
>
> will not necessarily equal
>
> a xor (b xor c)
>
> To see that this is the case, let a= 1, b= 2, and c= 3.
>
> (a xor b) xor c
>
> yields 3, while
>
> a xor (b xor c)
>
> yields 1. So, I'd prefer an xor operator that simply returns True or
False.
>
> Phillip
>
You are, of course, free to write your version however it makes sense to
you and your team. :)
Since the 'and' and 'or' already return objects (and objects evaluate to
true or false), then 'xor' should behave likewise, IMO. I expect that
would be the case if it were ever added to the language.
~Ethan~
More information about the Python-list
mailing list