Does Python need an 'xor' operator?

Roy Smith roy at panix.com
Sun Apr 14 14:11:42 EDT 2002


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.

Well, you could look at it as "you only evaluate as many operands, from 
left to right, as you need to determine the value of the expression".  This 
describes the current behavior of "and" and "or", and with only a small 
stretch, could be said to describe "xor" as well.  This may be a little far 
fetched, but, how about:

x = {'foo': 0}
y = x['bar'] xor x(0)

would be guaranteed to raise a KeyError and not a TypeError, since the 
left-to-right evaluation would ensure that x(0) was never evaluated.



More information about the Python-list mailing list