missing 'xor' Boolean operator

Ethan Furman ethan at stoneleaf.us
Fri Jul 17 16:51:05 CEST 2009


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_3
>>
>> The equivalent:
>>
>> failed = (result_1 != 0) or (result_2 != 0) or (result_3 != 0)
>> # or if you prefer:
>> succeeded = (result_1 == 0) and (result_2 == 0) and (result_3 == 0)
>>
>>   
> 
> [snip]
> 
> This is, I guess, where we disagree. I find the second proposal less 
> error prone, and universally understandable unlike the first one. 

Careful!  The very few (if any) things in this world that can be 
considered "universally understandable" do *not* include any programming 
language!  :)

> It may 
> be verbose, it may look even lame to some people, but in the end this is 
> perfectly reliable, because you manipulate only False or True within the 
> boolean operations.
> 
> The first form does not clearly show what is the failed criteria. It 
> just happens by coincidence that in this case the failed criteria 
> matches the Nothingness of result_1, result_2, result_3. What if results 
> may be 'OK' or 'KO'.

As the programmer, particularly a Python programmer, you should be 
taking advantage of Python's strengths, of which this is one.  If, as 
you say, some function uses a "something" value to indicate an 
undesirable result, then you have to use something akin to your last 
statement.


~Ethan~

> 
> failed = result_1 or result_2 or result_3
> won't work.
> 
> failed = (result_1 =='KO') or (result_2 =='KO') or (result_3 =='KO') is 
> lame but reliable.
> 
> 
> JM




More information about the Python-list mailing list