MRAB wrote:
Ethan Furman wrote:

Robert Kern wrote:

On 2009-07-14 14:56, Dr. Phillip M. Feldman wrote:

!= does do what I want, except that it doesn't indicate to someone reading the code that the operands are being treated as logicals. (Readability is supposed to be one of the major selling points of Python). But, this is
probably good enough.



In the words of those greater than myself, "Not every one-liner needs to be in the standard library."

def xor(a, b):
    return bool(a) != bool(b)


Let's see...

  and returns the last object that is "true"
  or  returns the first object that is "true"

so should xor return the only object that is "true", else False/None?

def xor(a, b)
    if a and b:
        return None
    elif a:
        return a
    elif b:
        return b
    else:
        return None

How about:

def xor(a, b):
    return not b and a or not a and b

In [12]: not 1 and 0 or 1 and not 0
Out[12]: True

In [13]: not 0 and 0 or 0 and not 0
Out[13]: 0

In [14]: not 1 and 1 or 1 and not 1
Out[14]: False

In [15]: not [] and [] or [] and not []
Out[15]: []

Doesn't produce consistent objects, sometimes bool, sometimes something else. 'Course, it all depends on what you need.

~Ethan~
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to