Does Python need an 'xor' operator?

Grant Edwards grante at visi.com
Sun Apr 14 09:53:33 EDT 2002


In article <m31ydiy7b9.fsf at mira.informatik.hu-berlin.de>, Martin v. Loewis wrote:
> grante at visi.com (Grant Edwards) writes:
> 
>> I agree.  Since the other boolean operators are "not" "and"
>> "or", it would be a bit of a surprise that one doesn't use
>> "xor". And having to convert a boolean to a "chunk of bits"
>> first and do a bitwise xor on it is just plain ugly.
> 
> As Bengt points out, it is tricky to define this analogous to 'and'
> and 'or'. Both have a shortcut meaning, evaluating the second one only
> if the first does not determine the outcome. Furthermore, they both
> return the last expression evaluated. This is not possible for 'xor'

Obviously.  That's implied by the definition of the operation.

> you have to evaluate both, and there is no clear choice as to which
> one is better as a result - in fact, the OP wants it to return bool.
> 
> So xor would be very much *unlike* 'and' and 'or'.

Point is that "^" operates bitwise, and there is no
exclusive-or operation for "truth" values.  Returning a bool
seems like the expected behavior to me.

-- 
Grant Edwards                   grante             Yow!  .. someone in DAYTON,
                                  at               Ohio is selling USED
                               visi.com            CARPETS to a SERBO-CROATIAN



More information about the Python-list mailing list