Why can't I xor strings?

Grant Edwards grante at visi.com
Sun Oct 10 11:30:32 EDT 2004


On 2004-10-10, Andrew Dalke <adalke at mindspring.com> wrote:

> Why the distinction?  In your code you call bool on
> an object at least once and perhaps twice.  The
> truth of an object should only be checked once.  You
> also have asymmetric return values.  Consider
>
>    s1   s2    s1 xor s2
>    "A"  "B"    False
>    "A"  ""     True
>    ""   "B"    "B"
>    ""   ""     False

Hey, _that_ particular bug isn't my fault.  Somebody else
decided that "and" and "or" don't return boolean values like
they should. ;)

> Esthetics suggest that either "A"/"" return "A" or that
> ""/"B" return True.  Mine does the latter.  Yours does
> neither.  Probably the Pythonic way, assuming 'xor'
> can be considered Pythonic, is to return the object
> which gave the True value, like this
>
> def xor_f(x, y):
>    bx = bool(x)
>    by = bool(y)
>    if bx:
>      if not by:
>        return bx
>      return False
>    else:
>      if by:
>        return by
>    return False

That's ugly.  But then again, "A" or "B" returning "A" is ugly.

While I agree with your points, they're immaterial to the
argument I was making.  The poster to which I responded was
arguing that "xor" didn't make sense because having it coerce
it's arguments to booleans was "wrong".

-- 
Grant Edwards                   grante             Yow!  Zippy's brain cells
                                  at               are straining to bridge
                               visi.com            synapses...



More information about the Python-list mailing list