missing 'xor' Boolean operator

Ethan Furman ethan at stoneleaf.us
Tue Jul 21 11:34:19 EDT 2009


Mark Dickinson wrote:
> 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


Very good question -- I likewise do not know the answer.  I will only 
observe that any() and all() are functions, while 'and' and 'or' are 
not.  If one wanted the object-returning behavior one could string 
together 'or's or 'and's instead.

~Ethan~
--
Thinking out loud here -- YMMV.  :)



More information about the Python-list mailing list