missing 'xor' Boolean operator
Emile van Sebille
emile at fenx.com
Fri Jul 17 12:55:39 EDT 2009
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 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)
> 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
No -- it happens by design because the premise is 'where 0 means "no
error" and an arbitrary non-zero integer means some error'.
> 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'.
Which by definition won't happen for the example cited...
> failed = result_1 or result_2 or result_3
> won't work.
... so you certainly wouldn't write a test that couldn't properly
determine success or failure.
> failed = (result_1 =='KO') or (result_2 =='KO') or (result_3 =='KO') is
> lame but reliable.
In this case I'd write something that meets the specs of the problem
failed = 'KO' in (result_1,result_2,result_3)
More information about the Python-list