Why can't I xor strings?

Grant Edwards grante at visi.com
Sat Oct 9 18:22:41 EDT 2004


On 2004-10-09, David Bolen <db3l at fitlinxx.com> wrote:

>>> The basic problem is that there is no obvious "xor on string"
>>> operation
>> 
>> Sure there is.  Strings have a boolean value, and the xor
>> operation on boolean values is well-defined.
>
> That's an operation, but I'm not sure that's the obvious one.
> For my part, if I saw "string1 ^ string2" I'd probably expect
> a byte by byte xor with the result being a new string.

Only because Python lacks a logical xor operator, so you're
used to thinking of ^ as a bitwise operator.  What if you saw

  string1 xor string2?

Wouldn't you expect it to be equivalent to

  (string1 and (not string2)) or ((not string1) and string2)
  
> It doesn't feel natural to me to have my strings suddenly
> interpreted as a new data type based on the operation at hand.
> Logical operators work that way but not numerics

I don't know what you mean by that.  Nobody seems to have a
problem with "and" "or" and "not" operators using the truth
values of strings.  What is there about "xor" that precludes it
from behaving similarly?

> (it would be in the same vein as string + number interpreting
> the string as a number - that way lies Perl :-))

I don't see that at all.

-- 
Grant Edwards                   grante             Yow!  Let's go to CHURCH!
                                  at               
                               visi.com            



More information about the Python-list mailing list