Why can't I xor strings?

Alex Martelli aleaxit at yahoo.com
Mon Oct 11 15:15:19 CEST 2004


Grant Edwards <grante at visi.com> wrote:
   ...
> Probably so, but that doesn't support the arguement that
> there's something wrong with a logical xor argument coercing
> it's operands to boolean values unless one also argues that
> the logical "and", "or" and "not" operators should also not
> coerce their operands to booleans.

One of the greatest sources of usefulness for 'and' and 'or' is exactly
that these operators _don't_ coerce their operands -- they always return
one of the objects that are given as their operands, never any
'coercion' of it.  This lets one code, e.g., 

    k, v = (onedict or another).popitem()

instead of

    if onedict:
        k, v = onedict.popitem()
    else:
        k, v = another.popitem()

Operator 'xor' couldn't possibly ensure this useful behavior, alas.


Alex



More information about the Python-list mailing list