Why can't I xor strings?

Stephen Waterbury golux at comcast.net
Sat Oct 9 18:26:57 EDT 2004


David Bolen wrote:
> Grant Edwards <grante at visi.com> writes:
>>On 2004-10-09, Jeremy Bowers <jerf at jerf.org> 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.

... but you'd get a traceback.  ;)  As pointed out earlier in this
thread, what works is "bool(string1) ^ bool(string2)", which
certainly doesn't violate the law of least astonishment.
Why would anything else be needed?



More information about the Python-list mailing list