missing 'xor' Boolean operator

Mark Dickinson dickinsm at gmail.com
Tue Jul 21 10:51:50 CEST 2009


On Jul 20, 11:34 pm, Ethan Furman <et... at stoneleaf.us> 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
>  >
>  > will not necessarily equal
>  >
>  > a xor (b xor c)
>  >
>  > To see that this is the case, let a= 1, b= 2, and c= 3.
>  >
>  > (a xor b) xor c
>  >
>  > yields 3, while
>  >
>  > a xor (b xor c)
>  >
>  > yields 1.  So, I'd prefer an xor operator that simply returns True or
> False.
>  >
>  > Phillip
>  >
>
> You are, of course, free to write your version however it makes sense to
> you and your team.  :)
>
> 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 all()
functions introduced in 2.5 return a boolean rather than returning
one of their arguments?  (I did, and I'm still not sure what the
answer is.)

Mark



More information about the Python-list mailing list