Does Python need an 'xor' operator?

Ken Peek Ken.Peek at SpiritSongDesigns.comNOSPAM
Mon Apr 15 11:02:21 EDT 2002


"Steve Holden" <sholden at holdenweb.com> wrote in message
news:LHAu8.11094$Ji7.10494 at atlpnn01.usenetserver.com...

| "Bengt Richter" <bokr at oz.net> wrote ...
|
| Since in Boolean terms a xor b is equivalent to ((a and not b) or (b and not
| a)), maybe a xor b should return whichever of a or b is not false.
|
| >>> a = 42
| >>> b = 0
| >>> a ^ b
| 42
| >>> b ^ a
| 42
| >>>
|
| Since that's exactly what it appears to do, stop fiddling with what already
| works !

NO it DOESN'T!! ::

>>> a = "123"
>>> b = 0
>>> a ^ b
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for ^: 'str' and 'int'
>>>

'xor' should return the string '123' in this case-- not an error.  In other
words, '^' deals with BITS (as it now does), and 'xor' deals with objects.

I need (in my work) the ability to diddle bits-- these operators need to "do the
good thing" when operating on ints (and longs).  I don't want these operators
overloaded to recognize other objects-- that is the purpose of 'not', 'or',
'and', (and would be operator 'xor').

The operator '!=' performs a similar operation, but always returns a boolean.
'xor' should return the non-false object if the other operand is false, and
false if both operands are either true or both operands are false.

The 3 different behaviors of '^', '!=', and 'xor' each have their place in
writing code...






More information about the Python-list mailing list