Re: missing 'xor' Boolean operator

2009-07-27 Thread Mark Dickinson
On Jul 27, 1:53 am, "Delaney, Timothy (Tim)" wrote: > Mark Dickinson wrote: > >> Since the 'and' and 'or' already return objects (and objects > >> evaluate to true or false), then 'xor' should behave likewise, IMO. > >> I expect that would be the case if it were ever added to the > >> language. >

Re: missing 'xor' Boolean operator

2009-07-26 Thread Terry Reedy
greg wrote: Terry Reedy wrote: In Math and Python, a Although Python extends the chaining principle to !=, this is somewhat questionable, because a < b and b < c implies a < c, but a != b and b != c does not imply a != c. I'm not sure I've ever seen a mathematician write a != b != c, but if I

Re: missing 'xor' Boolean operator

2009-07-26 Thread greg
Terry Reedy wrote: In Math and Python, a Although Python extends the chaining principle to !=, this is somewhat questionable, because a < b and b < c implies a < c, but a != b and b != c does not imply a != c. I'm not sure I've ever seen a mathematician write a != b != c, but if I did, I would

RE: missing 'xor' Boolean operator

2009-07-26 Thread Delaney, Timothy (Tim)
Mark Dickinson wrote: >> Since the 'and' and 'or' already return objects (and objects >> evaluate to true or false), then 'xor' should behave likewise, IMO. >> I expect that would be the case if it were ever added to the >> language. > > I'm not so sure. Did you ever wonder why the any() and al

Re: missing 'xor' Boolean operator

2009-07-25 Thread Terry Reedy
Albert van der Horst wrote: Remains whether we need an xor that only works and requires that both operands are booleans. That one we have already! It is called != . (a!=b)!=c and a!=(b!=c) are the same for booleans, so can indeed be expressed a!=b!=c (associativy of xor) Not in Python >>>

Re: missing 'xor' Boolean operator

2009-07-25 Thread Albert van der Horst
In article , Jean-Michel Pichavant wrote: >Christian Heimes wrote: >> Chris Rebert wrote: >> >>> Using the xor bitwise operator is also an option: >>> bool(x) ^ bool(y) >>> >> >> I prefer something like: >> >> bool(a) + bool(b) == 1 >> >> It works even for multiple tests (super xor): >> >>

Re: missing 'xor' Boolean operator

2009-07-21 Thread Ethan Furman
Mark Dickinson wrote: On Jul 20, 11:34 pm, Ethan Furman wrote: Dr. Phillip M. Feldman wrote: > Suppose that 'xor' returns the value that is true when only one value is > true, and False otherwise. This definition of xor doesn't have the standard > associative property, that is, > > (a xor b)

Re: missing 'xor' Boolean operator

2009-07-21 Thread Mark Dickinson
On Jul 20, 11:34 pm, Ethan Furman wrote: > Dr. Phillip M. Feldman wrote: >  > Suppose that 'xor' returns the value that is true when only one value is >  > true, and False otherwise.  This definition of xor doesn't have the > standard >  > associative property, that is, >  > >  > (a xor b) xor c >

Re: missing 'xor' Boolean operator

2009-07-20 Thread Ethan Furman
[fixed for bottom-posting] Dr. Phillip M. Feldman wrote: MRAB-2 wrote: What values should 'xor' return? IMHO, if only one of the values is true then it should return that value, otherwise it should return False. 1 xor 0 => 1 0 xor 2 => 2 1 xor 2 => False 0 xor 0 => False T

Re: missing 'xor' Boolean operator

2009-07-18 Thread Mark Dickinson
On Jul 17, 12:06 pm, Jean-Michel Pichavant wrote: > I was saying that using boolean operators with object instead of boolean > values is error prone, I agree with this to some extent. After all, Python conditional expressions were eventually introduced in response to buggy uses of the 'a and b o

Re: missing 'xor' Boolean operator

2009-07-17 Thread Dr. Phillip M. Feldman
Suppose that 'xor' returns the value that is true when only one value is true, and False otherwise. This definition of xor doesn't have the standard associative property, that is, (a xor b) xor c will not necessarily equal a xor (b xor c) To see that this is the case, let a= 1, b= 2, and c= 3

Re: missing 'xor' Boolean operator

2009-07-17 Thread Emile van Sebille
On 7/17/2009 7:34 AM Jean-Michel Pichavant said... Steven D'Aprano wrote: On Thu, 16 Jul 2009 15:53:45 +0200, Jean-Michel Pichavant wrote: Given three result codes, where 0 means "no error" and an arbitrary non- zero integer means some error, it is simple and easy to write: failed = result_1

Re: missing 'xor' Boolean operator

2009-07-17 Thread Steven D'Aprano
On Fri, 17 Jul 2009 16:34:57 +0200, Jean-Michel Pichavant wrote: > Steven D'Aprano wrote: >> On Thu, 16 Jul 2009 15:53:45 +0200, Jean-Michel Pichavant wrote: >> >> >> Given three result codes, where 0 means "no error" and an arbitrary >> non- zero integer means some error, it is simple and easy to

Re: missing 'xor' Boolean operator

2009-07-17 Thread Ethan Furman
Jean-Michel Pichavant wrote: Steven D'Aprano wrote: On Thu, 16 Jul 2009 15:53:45 +0200, Jean-Michel Pichavant wrote: Given three result codes, where 0 means "no error" and an arbitrary non- zero integer means some error, it is simple and easy to write: failed = result_1 or result_2 or result

Re: missing 'xor' Boolean operator

2009-07-17 Thread Jean-Michel Pichavant
Steven D'Aprano wrote: On Thu, 16 Jul 2009 15:53:45 +0200, Jean-Michel Pichavant wrote: Given three result codes, where 0 means "no error" and an arbitrary non- zero integer means some error, it is simple and easy to write: failed = result_1 or result_2 or result_3 The equivalent: failed = (

Re: missing 'xor' Boolean operator

2009-07-17 Thread Luis Zarrabeitia
On Friday 17 July 2009 07:06:26 am Jean-Michel Pichavant wrote: > > I was saying that using boolean operators with object instead of boolean > values is error prone, cause no language behaves he same way, I don't know of many languages that actively promote the duck typing concept, are as highly

Re: missing 'xor' Boolean operator

2009-07-17 Thread Steven D'Aprano
On Thu, 16 Jul 2009 15:53:45 +0200, Jean-Michel Pichavant wrote: > Python has extended the algebra definition of "or" and "and" top any > type, but it is so unintuitive (I'm no LISP programmer). I disagree. The Something/Nothing dichotomy is so intuitive to me that I would hate to go back to a l

Re: missing 'xor' Boolean operator

2009-07-17 Thread Jean-Michel Pichavant
Luis Alberto Zarrabeitia Gomez wrote: Quoting Jean-Michel Pichavant : Emile van Sebille wrote: On 7/16/2009 7:04 AM Unknown said... On 2009-07-16, Emile van Sebille wrote: daysInAdvance = int(inputVar) or 25 I don't get it. That doesn't work right when i

Re: missing 'xor' Boolean operator

2009-07-16 Thread Nobody
On Thu, 16 Jul 2009 14:59:22 -0700, Emile van Sebille wrote: If the question was "Why is there no 'or' operator ?", would "because A or B <=> not(not A and not B)" be a proper answer ? >>> Note that in Python A or B is in fact not equivalent to not(not A and >>> not B). >> >> Ah, but it

Re: missing 'xor' Boolean operator

2009-07-16 Thread Emile van Sebille
On 7/16/2009 1:29 PM Nobody said... On Wed, 15 Jul 2009 18:14:10 +0200, Hrvoje Niksic wrote: If the question was "Why is there no 'or' operator ?", would "because A or B <=> not(not A and not B)" be a proper answer ? Note that in Python A or B is in fact not equivalent to not(not A and not B).

Re: missing 'xor' Boolean operator

2009-07-16 Thread Nobody
On Wed, 15 Jul 2009 18:14:10 +0200, Hrvoje Niksic wrote: >> If the question was "Why is there no 'or' operator ?", would "because >> A or B <=> not(not A and not B)" be a proper answer ? > > Note that in Python A or B is in fact not equivalent to not(not A and > not B). Ah, but it *is* "equivale

Re: missing 'xor' Boolean operator

2009-07-16 Thread Nobody
On Thu, 16 Jul 2009 11:06:54 +0200, Jean-Michel Pichavant wrote: >>> So if I resume: >>> - not 'foo' => False >>> - 'foo' or 'foo' => 'foo' >>> >>> I may be missing something, but honestly, Guido must have smoked some >>> heavy stuff to write such logic, has he ? >> >> Several languages (e.g. Lis

Re: missing 'xor' Boolean operator

2009-07-16 Thread Luis Alberto Zarrabeitia Gomez
Quoting Jean-Michel Pichavant : > Emile van Sebille wrote: > > On 7/16/2009 7:04 AM Unknown said... > >> On 2009-07-16, Emile van Sebille wrote: > >>> daysInAdvance = int(inputVar) or 25 > >> > >> I don't get it. That doesn't work right when inputVar == "0". > >> > > Aah, but you didn't get to

Re: missing 'xor' Boolean operator

2009-07-16 Thread Jean-Michel Pichavant
Emile van Sebille wrote: On 7/16/2009 7:04 AM Unknown said... On 2009-07-16, Emile van Sebille wrote: daysInAdvance = int(inputVar) or 25 I don't get it. That doesn't work right when inputVar == "0". Aah, but you didn't get to define right. :) For that particular example 0 is not a vali

Re: missing 'xor' Boolean operator

2009-07-16 Thread Emile van Sebille
On 7/16/2009 7:04 AM Unknown said... On 2009-07-16, Emile van Sebille wrote: daysInAdvance = int(inputVar) or 25 I don't get it. That doesn't work right when inputVar == "0". Aah, but you didn't get to define right. :) For that particular example 0 is not a valid response. Emile -- ht

Re: missing 'xor' Boolean operator

2009-07-16 Thread Grant Edwards
On 2009-07-16, Emile van Sebille wrote: > On 7/16/2009 2:06 AM Jean-Michel Pichavant said... >> Ok then, why "or" does not return True, if the first element is >> considered True ? Why returning the element itself. Any reason for that >> ? Because it's confusing, maybe people used to that logic

Re: missing 'xor' Boolean operator

2009-07-16 Thread Jean-Michel Pichavant
Emile van Sebille wrote: On 7/16/2009 2:06 AM Jean-Michel Pichavant said... Ok then, why "or" does not return True, if the first element is considered True ? Why returning the element itself. Any reason for that ? Because it's confusing, maybe people used to that logic find it obvious, but I r

Re: missing 'xor' Boolean operator

2009-07-16 Thread Emile van Sebille
On 7/16/2009 2:06 AM Jean-Michel Pichavant said... Ok then, why "or" does not return True, if the first element is considered True ? Why returning the element itself. Any reason for that ? Because it's confusing, maybe people used to that logic find it obvious, but I really don't. For example

Re: missing 'xor' Boolean operator

2009-07-16 Thread Anthony Tolle
On Jul 15, 8:32 pm, Paul Rubin wrote: > Among other things, that uses quadratic time!  Why do you want to keep > popping items from that list instead of iterating through it anyway? > > Anyway, I think you wrote something close to this: > ... Very true! I didn't thi

Re: missing 'xor' Boolean operator

2009-07-16 Thread Jean-Michel Pichavant
Nobody wrote: On Wed, 15 Jul 2009 21:05:16 +0200, Jean-Michel Pichavant wrote: So if I resume: - not 'foo' => False - 'foo' or 'foo' => 'foo' I may be missing something, but honestly, Guido must have smoked some heavy stuff to write such logic, has he ? Several languages (e.g. Lisp,

Re: missing 'xor' Boolean operator

2009-07-16 Thread Lino Mastrodomenico
2009/7/16 Hendrik van Rooyen : > "Hrvoje Niksic" wrote: > > >> Note that in Python A or B is in fact not equivalent to not(not A and >> not B). > > De Morgan would turn in his grave. If this can make him happier, in Python (not (not a and not b)) *is* equivalent to bool(a or b). (Modulo crazy thi

Re: missing 'xor' Boolean operator

2009-07-16 Thread Steven D'Aprano
On Thu, 16 Jul 2009 09:43:53 +0200, Hendrik van Rooyen wrote: > "Hrvoje Niksic" wrote: > > >> Note that in Python A or B is in fact not equivalent to not(not A and >> not B). > > De Morgan would turn in his grave. No he wouldn't. Python isn't Boolean algebra, and there is no requirement to l

Re: missing 'xor' Boolean operator

2009-07-16 Thread Hendrik van Rooyen
"Hrvoje Niksic" wrote: > Note that in Python A or B is in fact not equivalent to not(not A and > not B). De Morgan would turn in his grave. - Hendrik -- http://mail.python.org/mailman/listinfo/python-list

Re: missing 'xor' Boolean operator

2009-07-15 Thread Steven D'Aprano
On Wed, 15 Jul 2009 21:05:16 +0200, Jean-Michel Pichavant wrote: > Didn't know that. > So if I resume: > - not 'foo' => False > - 'foo' or 'foo' => 'foo' > > I may be missing something, but honestly, Guido must have smoked some > heavy stuff to write such logic, has he ? No, it's perfectly reaso

Re: missing 'xor' Boolean operator

2009-07-15 Thread Paul Rubin
Anthony Tolle writes: > def xor(*operands): > if operands: > operands = list(operands) > a = bool(operands.pop(0)) > while operands: > b = bool(operands.pop(0)) > if a: > if b: > a = False > elif b:

Re: missing 'xor' Boolean operator

2009-07-15 Thread Nobody
On Wed, 15 Jul 2009 21:05:16 +0200, Jean-Michel Pichavant wrote: > So if I resume: > - not 'foo' => False > - 'foo' or 'foo' => 'foo' > > I may be missing something, but honestly, Guido must have smoked some > heavy stuff to write such logic, has he ? Several languages (e.g. Lisp, Bourne shell)

Re: missing 'xor' Boolean operator

2009-07-15 Thread Chris Rebert
On Wed, Jul 15, 2009 at 3:57 PM, Wayne Brehaut wrote: > On 15 Jul 2009 09:11:44 GMT, Steven D'Aprano > wrote: > >>On Tue, 14 Jul 2009 11:25:08 -0700, Dr. Phillip M. Feldman wrote: >> >>> Current Boolean operators are 'and', 'or', and 'not'.  It would be nice >>> to have an 'xor' operator as well.

Re: missing 'xor' Boolean operator

2009-07-15 Thread Wayne Brehaut
On 15 Jul 2009 09:11:44 GMT, Steven D'Aprano wrote: >On Tue, 14 Jul 2009 11:25:08 -0700, Dr. Phillip M. Feldman wrote: > >> Current Boolean operators are 'and', 'or', and 'not'. It would be nice >> to have an 'xor' operator as well. > >I've often wished there was too, for the sake of completenes

Re: missing 'xor' Boolean operator

2009-07-15 Thread Wayne Brehaut
On Tue, 14 Jul 2009 11:47:41 -0700 (PDT), Mark Dickinson wrote: >On Jul 14, 7:25 pm, "Dr. Phillip M. Feldman" >wrote: >> 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

Re: missing 'xor' Boolean operator

2009-07-15 Thread Hrvoje Niksic
Jean-Michel Pichavant writes: > Hrvoje Niksic wrote: > [snip] >> Note that in Python A or B is in fact not equivalent to not(not A and >> not B). >> l = [(True, True), (True, False), (False, True), (False, False)] for p in l: > ... p[0] or p[1] [...] Try with a different data se

Re: missing 'xor' Boolean operator

2009-07-15 Thread Wayne Brehaut
On Wed, 15 Jul 2009 11:51:44 -0700 (PDT), Mark Dickinson wrote: >On Jul 15, 7:29 pm, Wayne Brehaut wrote: >> On Tue, 14 Jul 2009 11:47:41 -0700 (PDT), Mark Dickinson >> wrote: >> >I'd also guess that 'xor' would be much less used than 'and' or 'or', >> >but maybe that's just a reflection of th

Re: missing 'xor' Boolean operator

2009-07-15 Thread Jean-Michel Pichavant
Miles Kaufmann wrote: On Jul 15, 2009, at 1:43 PM, Jean-Michel Pichavant wrote: Hrvoje Niksic wrote: [snip] Note that in Python A or B is in fact not equivalent to not(not A and not B). >>> l = [(True, True), (True, False), (False, True), (False, False)] >>> for p in l: ... p[0] or p[1]

Re: missing 'xor' Boolean operator

2009-07-15 Thread Anthony Tolle
On Jul 14, 2:25 pm, "Dr. Phillip M. Feldman" wrote: > Current Boolean operators are 'and', 'or', and 'not'.  It would be nice to > have an 'xor' operator as well. My $0.02 on this discussion: There would be nothing gained by having non-bitwise XOR operator. You can't short-circuit XOR, because y

Re: missing 'xor' Boolean operator

2009-07-15 Thread Mark Dickinson
On Jul 15, 7:29 pm, Wayne Brehaut wrote: > On Tue, 14 Jul 2009 11:47:41 -0700 (PDT), Mark Dickinson > wrote: > >I'd also guess that 'xor' would be much less used than 'and' or 'or', > >but maybe that's just a reflection of the sort of code that I tend to > >write. > > You're right about that!. I

Re: missing 'xor' Boolean operator

2009-07-15 Thread Robert Kern
On 2009-07-15 13:29, Wayne Brehaut wrote: On Tue, 14 Jul 2009 11:47:41 -0700 (PDT), Mark Dickinson wrote: On Jul 14, 7:25 pm, "Dr. Phillip M. Feldman" wrote: 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'

Re: missing 'xor' Boolean operator

2009-07-15 Thread Wayne Brehaut
On Tue, 14 Jul 2009 11:47:41 -0700 (PDT), Mark Dickinson wrote: >On Jul 14, 7:25 pm, "Dr. Phillip M. Feldman" >wrote: >> 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

Re: missing 'xor' Boolean operator

2009-07-15 Thread Dr. Phillip M. Feldman
I did initially ask for an infix xor operator, but eventually gave up on this. I like the first of your two one-line solutions below; this is clean and easy to understand. Thanks! I'd still like to be able to write an expression like '(a and b) xor (c and d) xor (e and f)', but it looks as thou

Re: missing 'xor' Boolean operator

2009-07-15 Thread Terry Reedy
Tim Golden wrote: I was pondering on this yesterday, and the only case I've come across in my code -- and it's reasonably common -- is checking that one and only one of two params has been passed. I have code which wants, say, an id or a name but doesn't want both. It's hardly difficult to write

Re: missing 'xor' Boolean operator

2009-07-15 Thread Miles Kaufmann
On Jul 15, 2009, at 1:55 PM, Emile van Sebille wrote: On 7/15/2009 10:43 AM Jean-Michel Pichavant said... Hrvoje Niksic wrote: [snip] Note that in Python A or B is in fact not equivalent to not(not A and not B). Did I make twice the same obvious error ? No -- but in the not(not... example

Re: missing 'xor' Boolean operator

2009-07-15 Thread Miles Kaufmann
On Jul 15, 2009, at 1:43 PM, Jean-Michel Pichavant wrote: Hrvoje Niksic wrote: [snip] Note that in Python A or B is in fact not equivalent to not(not A and not B). >>> l = [(True, True), (True, False), (False, True), (False, False)] >>> for p in l: ... p[0] or p[1] [snip] Did I make twic

Re: missing 'xor' Boolean operator

2009-07-15 Thread Emile van Sebille
On 7/15/2009 10:43 AM Jean-Michel Pichavant said... Hrvoje Niksic wrote: [snip] Note that in Python A or B is in fact not equivalent to not(not A and not B). >>> l = [(True, True), (True, False), (False, True), (False, False)] >>> for p in l: ... p[0] or p[1] ... True True True False

Re: missing 'xor' Boolean operator

2009-07-15 Thread Jean-Michel Pichavant
Hrvoje Niksic wrote: [snip] Note that in Python A or B is in fact not equivalent to not(not A and not B). >>> l = [(True, True), (True, False), (False, True), (False, False)] >>> for p in l: ... p[0] or p[1] ... True True True False >>> for p in l: ... not(not p[0] and not p[1]) ... T

Re: missing 'xor' Boolean operator

2009-07-15 Thread Wayne Brehaut
On Wed, 15 Jul 2009 13:37:22 +0200, Christian Heimes wrote: >pdpi wrote: >> On Jul 15, 12:08 am, Christian Heimes wrote: >>> Chris Rebert wrote: Using the xor bitwise operator is also an option: bool(x) ^ bool(y) >>> I prefer something like: >>> >>> bool(a) + bool(b) == 1 >>> >>> I

Re: missing 'xor' Boolean operator

2009-07-15 Thread Paul Rubin
Hrvoje Niksic writes: > > While everyone's trying to tell the OP how to workaround the missing > > xor operator, nobody answered the question "why is there no [boolean] > > xor operator ?". > > Probably because there isn't one in C. The bitwise XOR operator, on the > other hand, exists in both C

Re: missing 'xor' Boolean operator

2009-07-15 Thread Hrvoje Niksic
Jean-Michel Pichavant writes: > While everyone's trying to tell the OP how to workaround the missing > xor operator, nobody answered the question "why is there no [boolean] > xor operator ?". Probably because there isn't one in C. The bitwise XOR operator, on the other hand, exists in both C an

Re: missing 'xor' Boolean operator

2009-07-15 Thread Robert Kern
On 2009-07-15 10:15, Jean-Michel Pichavant wrote: Christian Heimes wrote: Chris Rebert wrote: Using the xor bitwise operator is also an option: bool(x) ^ bool(y) I prefer something like: bool(a) + bool(b) == 1 It works even for multiple tests (super xor): if bool(a) + bool(b) + bool(c) + b

Re: missing 'xor' Boolean operator

2009-07-15 Thread Jean-Michel Pichavant
Christian Heimes wrote: Chris Rebert wrote: Using the xor bitwise operator is also an option: bool(x) ^ bool(y) 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("

Re: missing 'xor' Boolean operator

2009-07-15 Thread Bill Davy
"MRAB" wrote in message news:mailman.3158.1247667680.8015.python-l...@python.org... > Steven D'Aprano wrote: >> On Tue, 14 Jul 2009 11:25:08 -0700, Dr. Phillip M. Feldman wrote: >> >>> Current Boolean operators are 'and', 'or', and 'not'. It would be nice >>> to have an 'xor' operator as well. >

Re: missing 'xor' Boolean operator

2009-07-15 Thread MRAB
Steven D'Aprano wrote: On Tue, 14 Jul 2009 11:25:08 -0700, Dr. Phillip M. Feldman wrote: Current Boolean operators are 'and', 'or', and 'not'. It would be nice to have an 'xor' operator as well. I've often wished there was too, for the sake of completeness and aesthetics, I'd love to be abl

Re: missing 'xor' Boolean operator

2009-07-15 Thread Hendrik van Rooyen
"Steven D'Aprano" wrote: >Unfortunately, outside of boolean algebra and simulating electrical >circuits, I can't think of any use-cases for an xor operator. Do you have >any? A bitwise xor is a poor man's comparator - if the result is binary zero, the operands were equal, no matter what they

Re: missing 'xor' Boolean operator

2009-07-15 Thread pdpi
On Jul 15, 12:37 pm, Christian Heimes wrote: > pdpi wrote: > > On Jul 15, 12:08 am, Christian Heimes wrote: > >> Chris Rebert wrote: > >>> Using the xor bitwise operator is also an option: > >>> bool(x) ^ bool(y) > >> I prefer something like: > > >>     bool(a) + bool(b) == 1 > > >> It works even

Re: missing 'xor' Boolean operator

2009-07-15 Thread Christian Heimes
pdpi wrote: > On Jul 15, 12:08 am, Christian Heimes wrote: >> Chris Rebert wrote: >>> Using the xor bitwise operator is also an option: >>> bool(x) ^ bool(y) >> I prefer something like: >> >> bool(a) + bool(b) == 1 >> >> It works even for multiple tests (super xor): >> >> if bool(a) + bool(b

Re: missing 'xor' Boolean operator

2009-07-15 Thread Tim Golden
Steven D'Aprano wrote: On Tue, 14 Jul 2009 11:25:08 -0700, Dr. Phillip M. Feldman wrote: Current Boolean operators are 'and', 'or', and 'not'. It would be nice to have an 'xor' operator as well. I've often wished there was too, for the sake of completeness and aesthetics, I'd love to be abl

Re: missing 'xor' Boolean operator

2009-07-15 Thread Tim Wintle
On Wed, 2009-07-15 at 02:02 -0700, Jonathan Gardner wrote: > On Jul 14, 4:48 pm, Ethan Furman wrote: > > > > A whole family of supers. :) > > > All the things binary operators can do, Lisp > does with 0, 1, 2, or more arguments. +1 n-ary operators are great, but binary operators are just synt

Re: missing 'xor' Boolean operator

2009-07-15 Thread pdpi
On Jul 15, 12:08 am, Christian Heimes wrote: > Chris Rebert wrote: > > Using the xor bitwise operator is also an option: > > bool(x) ^ bool(y) > > 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

Re: missing 'xor' Boolean operator

2009-07-15 Thread Steven D'Aprano
On Tue, 14 Jul 2009 11:25:08 -0700, Dr. Phillip M. Feldman wrote: > Current Boolean operators are 'and', 'or', and 'not'. It would be nice > to have an 'xor' operator as well. I've often wished there was too, for the sake of completeness and aesthetics, I'd love to be able to write: a xor b i

Re: missing 'xor' Boolean operator

2009-07-15 Thread Jonathan Gardner
On Jul 14, 4:48 pm, Ethan Furman wrote: > > A whole family of supers.  :) > You should pick up Lisp. The whole concept of a binary operator doesn't exist over there. 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]> (+

Re: missing 'xor' Boolean operator

2009-07-15 Thread Asun Friere
On Jul 15, 5:44 pm, Mark Dickinson wrote: > On Jul 15, 5:07 am, "Dr. Phillip M. Feldman" > 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): ...". > Ah yes, but not once conditional tests,

Re: missing 'xor' Boolean operator

2009-07-15 Thread Ethan Furman
Scott David Daniels wrote: 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,"

Re: missing 'xor' Boolean operator

2009-07-15 Thread Mark Dickinson
On Jul 15, 5:07 am, "Dr. Phillip M. Feldman" wrote: > 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 nu

Re: missing 'xor' Boolean operator

2009-07-14 Thread Tim Roberts
"Dr. Phillip M. Feldman" wrote: > >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 dif

Re: missing 'xor' Boolean operator

2009-07-14 Thread Miles Kaufmann
On Jul 15, 2009, at 12:07 AM, Dr. Phillip M. Feldman wrote: 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) You originally proposed this in the c

Re: missing 'xor' Boolean operator

2009-07-14 Thread Dr. Phillip M. Feldman
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 (see ww

Re: missing 'xor' Boolean operator

2009-07-14 Thread Ethan Furman
Christian Heimes wrote: Chris Rebert wrote: Using the xor bitwise operator is also an option: bool(x) ^ bool(y) 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("Exactl

Re: missing 'xor' Boolean operator

2009-07-14 Thread Scott David Daniels
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

Re: missing 'xor' Boolean operator

2009-07-14 Thread Christian Heimes
Chris Rebert wrote: > Using the xor bitwise operator is also an option: > bool(x) ^ bool(y) 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 mus

Re: missing 'xor' Boolean operator

2009-07-14 Thread Ethan Furman
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 sellin

Re: missing 'xor' Boolean operator

2009-07-14 Thread MRAB
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 Py

Re: missing 'xor' Boolean operator

2009-07-14 Thread Ethan Furman
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

Re: missing 'xor' Boolean operator

2009-07-14 Thread Robert Kern
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.

Re: missing 'xor' Boolean operator

2009-07-14 Thread Chris Rebert
> Mark Dickinson wrote: >> >> On Jul 14, 7:25 pm, "Dr. Phillip M. Feldman" >> wrote: >>> 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 suff

Re: missing 'xor' Boolean operator

2009-07-14 Thread Dr. Phillip M. Feldman
!= 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. Here's a related issue: I would like to see an optio

Re: missing 'xor' Boolean operator

2009-07-14 Thread Mark Dickinson
On Jul 14, 8:43 pm, Chris Rebert wrote: > On Tue, Jul 14, 2009 at 11:47 AM, Mark Dickinson wrote: > > (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) Good point. For some reason I expected bitwise operation

Re: missing 'xor' Boolean operator

2009-07-14 Thread Chris Rebert
On Tue, Jul 14, 2009 at 11:47 AM, Mark Dickinson wrote: > On Jul 14, 7:25 pm, "Dr. Phillip M. Feldman" > wrote: >> 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 cas

Re: missing 'xor' Boolean operator

2009-07-14 Thread Mark Dickinson
On Jul 14, 7:25 pm, "Dr. Phillip M. Feldman" wrote: > 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 '

missing 'xor' Boolean operator

2009-07-14 Thread Dr. Phillip M. Feldman
Current Boolean operators are 'and', 'or', and 'not'. It would be nice to have an 'xor' operator as well. -- View this message in context: http://www.nabble.com/missing-%27xor%27-Boolean-operator-tp24485116p24485116.html Sent from the Python - python-list mailing list archive at Nabble.com. --