boolean xor

Fredrik Lundh fredrik at effbot.org
Sun Jan 7 14:06:15 EST 2001


Aahz Maruch wrote:
> I need a boolean (not bit-wise) xor function.  I've got the following
> function, but my logic skills are rusty enough that I'm wondering if
> this is the "right" way to do it (I've validated that it produces the
> correct outputs, at least):
>
> def xor(a,b):
>     return not ( (a and b) or (not (a or b)) )

here's another way to do it:

def xor(a,b):
    from operator import truth
    return truth(a) ^ truth(b)

Cheers /F





More information about the Python-list mailing list