missing 'xor' Boolean operator

Jean-Michel Pichavant jeanmichel at sequans.com
Fri Jul 17 10:34:57 EDT 2009


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. 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'.

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