Does Python need an 'xor' operator?

Martin v. Loewis martin at v.loewis.de
Sun Apr 14 05:10:18 EDT 2002


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

Regards,
Martin



More information about the Python-list mailing list