missing 'xor' Boolean operator

Albert van der Horst albert at spenarnc.xs4all.nl
Sat Jul 25 13:27:52 EDT 2009


In article <mailman.3164.1247670958.8015.python-list at python.org>,
Jean-Michel Pichavant  <jeanmichel at sequans.com> 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 ?

No. I think it is because and/or can be extended to be sensible
in a context where objects can be used. (What others have expressed
as having short-circuit evaluation. So sce indeed is the underlying
reason that and/or can be extended sensibly to objects.)

Remains whether we need an xor that only works and requires that
both operands are booleans. That one we have already!
It is called != .

(a!=b)!=c
and
a!=(b!=c)

are the same for booleans, so can indeed be expressed
a!=b!=c   (associativy of xor)

>
>JM
>
Groetjes Albert

--
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert at spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst




More information about the Python-list mailing list