xor operator?

Thomas Wouters thomas at xs4all.net
Fri Jan 26 09:02:31 EST 2001


On Fri, Jan 26, 2001 at 05:51:41AM -0700, Def P wrote:

> I notice that there doesn't seem to be a boolean xor operator in Python. Does
> someone know the reasons behind this? 

Very few languages have a boolean xor operation. I don't know any off the
top of my head, but if I'd have to make a guess, I'd say that INTERCAL was
the only language I'm famialiar with, with a boolean XOR operation :)

The simple reason is 'it isn't shortcuttable'. That is, with 'and' and 'or',
whether or not to evaluate the second argument is based on the outcome of
the first. xor requires you to always evaluate both. In the specific Python
sense, the return value of the boolean xor operation would also be vague.

(In Python 'a and b' returns a if a is false, or otherwise b. Similarly, 'a
or b' returns a if a is true, otherwise b. That means you can do stuff like
'l = a or []'. What would 'a xor b' return ? A if a is true, but b is false,
B if b is true, a is false, that much is 'obvious'. Then what? Return a, or
b, if a and b are both false ? And what about if a and b are both true ?
return (a,b) ? ;-P)

And lastly, I only once had use for a boolean xor operator (in Perl, at the
time), and after realizing there wasn't one, I also realized I didn't really
want a boolean xor operator at all, I really wanted an if/elif/else
construction.

What construction do you need the boolean xor operation for ? Isn't it more
obvious to use an if/elif/else contstruction ?

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list