missing 'xor' Boolean operator

Robert Kern robert.kern at gmail.com
Wed Jul 15 11:38:55 EDT 2009


On 2009-07-15 10:15, Jean-Michel Pichavant wrote:
> Christian Heimes wrote:
>> Chris Rebert wrote:
>>> Using the xor bitwise operator is also an option:
>>> bool(x) ^ bool(y)
>>
>> I prefer something like:
>>
>> bool(a) + bool(b) == 1
>>
>> It works even for multiple tests (super xor):
>>
>> if bool(a) + bool(b) + bool(c) + bool(d) != 1:
>> raise ValueError("Exactly one of a, b, c and d must be true")
>>
>> Christian
>>
> While everyone's trying to tell the OP how to workaround the missing xor
> operator, nobody answered the question "why is there no xor operator ?".
>
> If the question was "Why is there no 'or' operator ?", would "because A
> or B <=> not(not A and not B)" be a proper answer ?

That's not the only answer the OP has been given. The most compelling answer is 
that an "xor" keyword cannot be implemented with similar semantics to "and" and 
"or", in particular short-circuiting and returning one of the actual inputs 
instead of a coerced bool.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list