boolean xor

Alex Martelli aleaxit at yahoo.com
Fri Jan 12 05:20:10 EST 2001


"Delaney, Timothy" <tdelaney at avaya.com> wrote in message
news:mailman.979258453.29329.python-list at python.org...
> 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.

Guess one could argue this way, yes.  But "do this if feasible" is
a pretty common metaparadigm -- it won't astonish anybody if 'this'
doesn't get done when not feasible:-).


> There's just no way to short circuit xor.

Of course not, but, what's that got to do with it?

> Your proposed method presumably continues as

(Function, not method, but I guess you knew that -- we overload
so many common words that it's extremely easy to hit one:-).

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

No, I explicitly mentioned leaving off this last line you're adding.
Why '0'?  Returning None seems more Pythonic here (and can get
accomplished by just having no third line in the function's body,
although I acknowledge that cutesy touch is quite doubtful).


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

Sorry, I don't follow you at all here.  E.g., "returning a reference"
is definitely NOT an "implementation detail" in Python -- it's a deep,
crucial part of its semantics; so, I don't get what you're driving at.

Considerations in C would of course be completely different (C does NOT
return original values, but rather 0 or 1, even from short-circuited
&& and || -- a design flaw, IMHO, but, to keep consistent with it, one
would NOT want to 'return original values' from an xor-function either).


Alex






More information about the Python-list mailing list