missing 'xor' Boolean operator
Scott David Daniels
Scott.Daniels at Acm.Org
Tue Jul 14 19:36:35 EDT 2009
Ethan Furman wrote:
> and returns the last object that is "true"
A little suspect this.
_and_ returns the first object that is not "true," or the last object.
> or returns the first object that is "true"
Similarly:
_or_ returns the first object that is "true," or the last object.
> so should xor return the only object that is "true", else False/None?
Xor has the problem that in two cases it can return neither of its args.
Not has behavior similar in those cases, and we see it returns False or
True. The Pythonic solution is therefore to use False.
> def xor(a, b)
> if a and b:
> return None
> elif a:
> return a
> elif b:
> return b
> else:
> return None
def xor(a, b):
if bool(a) == bool(b):
return False
else:
return a or b
Side-effect counting in applications of bool(x) is ignored here.
If minimizing side-effects is needed:
def xor(a, b):
if a:
if not b:
return a
elif b:
return b
return False
--Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list