boolean xor

Delaney, Timothy tdelaney at avaya.com
Thu Jan 11 19:11:33 EST 2001


Actually, I think it should work the other way. Because it is not *always*
possible to return one of the arguments, xor should never return either of
the arguments. This is the path of least-astonishment.

There's just no way to short circuit xor. Your proposed method presumably
continues as

 def xor(A,B):
     if not A: return B
     if not B: return A
     return 0

This may be better (returning only requires the implementation detail of
returning a reference), but it can also be worse (need to evaluate 2 values
after dereferencing them, plus then need to return a *different* reference).

I remember hashing this out on comp.lang.c quite a few years ago now ...

Tim Delaney
Avaya Australia
+61 2 9352 9079

> -----Original Message-----
> From: Alex Martelli [mailto:aleaxit at yahoo.com]
> Sent: Friday, 12 January 2001 3:53 AM
> To: python-list at python.org
> Subject: Re: boolean xor
> 
> 
> "Steve Holden" <sholden at holdenweb.com> wrote in message
> news:Tbi76.10504$X3.66627 at e420r-atl1.usenetserver.com...
>     [snip]
> > A possibly salient point which hasn't been explicitly stated in this
> thread
> > (but might have been, seventeen days ago, my aging memory 
> and general
> > decreptitude being what it is) is that the binary logical operators
> > short-circuit, and return the value of the last operand 
> they evaluated.
> Even
> 
> Bingo -- I *was* starting to think I should post about that.  
> For least-
> astonishment purposes, I'd thus like the xor function to 
> behave similarly:
> return either A or B if at all possible (it may not be, of course, if
> both A and B are true).  E.g.,
> 
> def xor(A,B):
>     if not A: return B
>     if not B: return A
> 
> (perhaps too cutesy in the attempted symmetry and in relying on the
> default fall-off-the-end to avoid an explicit return None...).
> 
> 
> Alex
> 
> 
> 
> -- 
> http://www.python.org/mailman/listinfo/python-list
> 




More information about the Python-list mailing list