boolean xor
Aahz Maruch
aahz at panix.com
Wed Jan 10 13:23:08 EST 2001
In article <93i7ne$5dk$1 at nnrp1.deja.com>, <ndev42 at yahoo.com> wrote:
>Nicolas removed that attribution for Aahz:
>>
>> 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)) )
>
>A boolean xor is a detector of differences. Knowing that, you can
>simply write:
>
>def xor(a,b):
> return not(a==b)
That works only if the test is against equality, not "truthfullness" by
Python's standards. For example, I was using my xor() function this
way:
class Token:
def __init__(self, URL=None, Shutdown=None):
if not xor(URL, Shutdown):
raise "Must assign one of URL or Shutdown"
In this scenario, one would expect URL to be a string and Shutdown to be
an integer; comparing for equality would be useless.
--
--- Aahz (Copyright 2001 by aahz at pobox.com)
Androgynous poly kinky vanilla queer het <*> http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6
Sometimes, you're not just out of left field, you're coming in all the
way from outer space. --Aahz
More information about the Python-list
mailing list