Why can't I xor strings?

Alex Martelli aleaxit at yahoo.com
Mon Oct 11 09:24:42 CEST 2004


Phil Frost <indigo at bitglue.com> wrote:

> ^ is the bitwise xor operator. Performing bitwise operations on strings
> doesn't make much sense, so it's invalid. You can define a logical xor
> function like so:
> 
> xor = lambda p, q: (p and not q) or (not p and q)
> 
> or equivalently:
> 
> xor = lambda p, q: bool(p) != bool(q)

or, equivalently and avoiding the gratuitous lambda,

def xor(p, q): return bool(p) != bool(q)


Alex



More information about the Python-list mailing list