Why can't I xor strings?

Jeremy Bowers jerf at jerf.org
Sat Oct 9 14:24:20 EDT 2004


On Fri, 08 Oct 2004 16:19:22 -0400, dataangel wrote:
> Regardless of whether this is the best implementation for detecting if 
> two strings are similar, I don't see why xor for strings shouldn't be 
> supported. Am I missing something?

The basic problem is that there is no obvious "xor on string" operation
*in general*, even if you stipulate "bitwise". In particular, what does it
mean to bitwise xor two different length strings?

"In general" is the key, here. You actually don't have strings,
conceptually, you have "tokens" or "commands" or something that happen to
be strings. I recommend creating a class to match this concept by
subclassing str... or even just write it directly without subclassing
string. You can then make __xor__(self, other) for that class do whatever
makes sense with your concept.

http://docs.python.org/ref/numeric-types.html

I can almost guarantee that you will later find other things to put into
that class. It may very well clean up a lot of code.



More information about the Python-list mailing list