xor operator?

Hans Nowak ivnowa at hvision.nl
Fri Jan 26 10:39:57 EST 2001


Thomas Wouters wrote:

I goofed (again) and mistook some of your answers for Nicola
Musatti's... :(
Anyway, I'll repeat them here:

> 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 :)

Pascal does. I'm not sure about standard Pascal (if there is such a
thing), but Turbo Pascal did, and Delphi does today, too.

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

I see the difference, but I don't understand why that is a problem,
though. 

> In the specific Python
> sense, the return value of the boolean xor operation would also be vague.

Simply 0 or 1 would be a possibility, but that is not what Python's
'and' and 'or' do. So maybe, if it evaluates to true, it returns the
'true' element; otherwise it returns 0 (like 'and' and 'or' do).

> (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)

I think it should return 0, since the expression as a whole evaluates to
false.

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

Can't speak for him, but I always wondered why the xor was missing, too.
It simply belongs there next to the and/&, or/| pairs. :)

--Hans Nowak
--Info Vision Europe BV



More information about the Python-list mailing list