Does Python need an 'xor' operator?

David Eppstein eppstein at ics.uci.edu
Sun Apr 14 16:52:16 EDT 2002


In article <m31ydiy7b9.fsf at mira.informatik.hu-berlin.de>,
 martin at v.loewis.de (Martin v. Loewis) wrote:

> 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':
> 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'.

If (a xor b) is true, it must be because exactly one of a and b is true.
So, in that case, it seems reasonable to return the one that is true.
I'm not sure what value would make most sense to return when (a xor b) 
is false, though.

I.e. (a xor b) could expand to something like
    (not b and a) or (not a and b)
but without all the repetition...

-- 
David Eppstein       UC Irvine Dept. of Information & Computer Science
eppstein at ics.uci.edu http://www.ics.uci.edu/~eppstein/



More information about the Python-list mailing list