D
Dr. Phillip M. Feldman
Current Boolean operators are 'and', 'or', and 'not'. It would be nice to
have an 'xor' operator as well.
have an 'xor' operator as well.
Current Boolean operators are 'and', 'or', and 'not'. It would be nice to
have an 'xor' operator as well.
Hmm. Â I don't think 'nice' is sufficient. Â You'd need to make the case
that it's sufficiently useful to justify adding a new keyword 'xor' to
the language; Â I suspect that would be an uphill struggle.
I'll just note that:
(1) It's easy to emulate xor: Â 'x xor y' <-> bool(x) != bool(y)
Using the xor bitwise operator is also an option:
bool(x) ^ bool(y)
Feldman said:Here's a related issue: I would like to see an option for type checking on
operands of logical operators, so that attempting to apply a logical
operator to non-Boolean entities generates a warning message. With operand
type checking, 'xor' and != would be different.
#empty containers considered false
bool([] or {} or set() or "")
False
#zero and null considered false
bool(0 or None)
False
#anything else is, by default, true
bool(object())
True
!= 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.
Robert said: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)
How about:Ethan said: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
MRAB said:How about:
def xor(a, b):
return not b and a or not a and b
Christian said:I prefer something like:
bool(a) + bool(b) == 1
It works even for multiple tests (super xor):
if bool(a) + bool(b) + bool(c) + bool(d) != 1:
raise ValueError("Exactly one of a, b, c and d must be true")
Christian
Scott said:A little suspect this.
_and_ returns the first object that is not "true," or the last object.
Similarly:
_or_ returns the first object that is "true," or the last object.
I appreciate the effort that people have made, but I'm not impressed
with any
of the answers. For one thing, xor should be able to accept an
arbitrary
number of input arguments (not just two)
and should return True if and only
if the number of input arguments that evaluate to True is odd
Here's my code:
def xor(*args):
"""xor accepts an arbitrary number of input arguments, returning
True
if and only if bool() evaluates to True for an odd number of the
input
arguments."""
result= False
for arg in args:
if bool(arg): result= not result
return result
I appreciate the effort that people have made, but I'm not impressed with any
of the answers. For one thing, xor should be able to accept an arbitrary
number of input arguments (not just two), and should return True if and only
if the number of input arguments that evaluate to True is odd.
Here's my code:
def xor(*args):
"""xor accepts an arbitrary number of input arguments, returning True
if and only if bool() evaluates to True for an odd number of the input
arguments."""
result= False
for arg in args:
if bool(arg): result= not result
return result
On Jul 15, 5:07 am, "Dr. Phillip M. Feldman" <[email protected]>
wrote:
[snip]
for arg in args:
if bool(arg): result= not result
It's more idiomatic to say "if arg: ..." rather than "if bool
(arg): ...".
A whole family of supers.![]()
Current Boolean operators are 'and', 'or', and 'not'. It would be nice
to have an 'xor' operator as well.
I prefer something like:
bool(a) + bool(b) == 1
It works even for multiple tests (super xor):
if bool(a) + bool(b) + bool(c) + bool(d) != 1:
raise ValueError("Exactly one of a, b, c and d must be true")
Christian
All the things binary operators can do, Lisp
does with 0, 1, 2, or more arguments.
[1]> (+)
0
[2]> (+ 1)
1
[3]> (+ 1 2)
3
[4]> (+ 1 2 3)
6
6sum([]) 0
sum([1]) 1
sum([1,2]) 3
sum([1,2,3])
Once you get used to that, binary operators don't seem so useful
anymore.
The equivalent in Python is dropping the operators and replacing them
with built-in functions that take 0, 1, 2, or more arguments.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.