Why does Python have a bitwise but not a logical xor operator? It's even weirder because boolean objects do have a __xor__ method. Does Py3k have an xor keyword? (I am using 2.6 due to NumPy.) (Yes I know BDFL is planning a moratorium on syntax.) S.M.
Sturla Molden schrieb:
Why does Python have a bitwise but not a logical xor operator?
How often do you need the xor operator?
It's even weirder because boolean objects do have a __xor__ method.
The reason for that is that they inherit from integers. That method is the usual bitwise integer xor.
Does Py3k have an xor keyword? (I am using 2.6 due to NumPy.)
No. Georg -- Thus spake the Lord: Thou shalt indent with four spaces. No more, no less. Four shall be the number of spaces thou shalt indent, and the number of thy indenting shall be four. Eight shalt thou not indent, nor either indent thou two, excepting that thou then proceed to four. Tabs are right out.
On Tue, Oct 27, 2009 at 6:46 PM, Georg Brandl <g.brandl@gmx.net> wrote:
Sturla Molden schrieb:
Why does Python have a bitwise but not a logical xor operator?
How often do you need the xor operator?
1) Technically, an operator is *never* needed, as its just syntactic sugar. 2) It sure would make crypto code look prettier, as we rely on xor operations extensively. Geremy Condra
geremy condra schrieb:
On Tue, Oct 27, 2009 at 6:46 PM, Georg Brandl <g.brandl@gmx.net> wrote:
Sturla Molden schrieb:
Why does Python have a bitwise but not a logical xor operator?
How often do you need the xor operator?
1) Technically, an operator is *never* needed, as its just syntactic sugar.
Exactly. Therefore it makes sense to select the most often used operations and add syntactic sugar for them. Boolean XOR is not one of them. (The thread linked by Mark enumerates a fair number of equivalent spellings, but the most important thing is that it's impossible to make the "xor" behave equivalent to "and" and "or" in terms of short-circuiting and returning one of the operands.)
2) It sure would make crypto code look prettier, as we rely on xor operations extensively.
We *do* have a bitwise xor. Georg -- Thus spake the Lord: Thou shalt indent with four spaces. No more, no less. Four shall be the number of spaces thou shalt indent, and the number of thy indenting shall be four. Eight shalt thou not indent, nor either indent thou two, excepting that thou then proceed to four. Tabs are right out.
On 2009-10-27 18:07 PM, geremy condra wrote:
On Tue, Oct 27, 2009 at 6:46 PM, Georg Brandl<g.brandl@gmx.net> wrote:
Sturla Molden schrieb:
Why does Python have a bitwise but not a logical xor operator?
How often do you need the xor operator?
1) Technically, an operator is *never* needed, as its just syntactic sugar. 2) It sure would make crypto code look prettier, as we rely on xor operations extensively.
No, it wouldn't. Crypto uses the bitwise xor which we already have an operator for: ^. As I stated in the referenced thread, to me, the most compelling reason there is no "xor" keyword to go with "and" and "or" is that one cannot make an xor that shares the same short-circuiting behavior. Or the behavior of returning one of the operand objects rather than a coerced bool. Without either of those behaviors, there is little benefit to having a keyword operator where a trivial one-liner will suffice. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
On Tue, Oct 27, 2009 at 7:18 PM, Robert Kern <robert.kern@gmail.com> wrote:
On 2009-10-27 18:07 PM, geremy condra wrote:
On Tue, Oct 27, 2009 at 6:46 PM, Georg Brandl<g.brandl@gmx.net> wrote:
Sturla Molden schrieb:
Why does Python have a bitwise but not a logical xor operator?
How often do you need the xor operator?
1) Technically, an operator is *never* needed, as its just syntactic sugar. 2) It sure would make crypto code look prettier, as we rely on xor operations extensively.
No, it wouldn't. Crypto uses the bitwise xor which we already have an operator for: ^.
Actually, I use it primarily in the public-key context, where bitwise comparison doesn't make a whole lot of sense.
As I stated in the referenced thread, to me, the most compelling reason there is no "xor" keyword to go with "and" and "or" is that one cannot make an xor that shares the same short-circuiting behavior. Or the behavior of returning one of the operand objects rather than a coerced bool. Without either of those behaviors, there is little benefit to having a keyword operator where a trivial one-liner will suffice.
I've always tried to avoid and/or in Python for exactly that behavior. You're right that it would be confusing, though. Geremy Condra
I'm not in favour of of adding an xor operator, but it seems to me it IS possible to make it behave somewhat analagously to 'and' and 'or' as far as what it returns: X xor Y evaluates to: X if bool(X)==True and bool(Y)==False Y if bool(Y)==True and bool(X)==False False if bool(X)==Bool(Y)==True Y if bool(X)==bool(Y)==False The last case is analagous to X or Y evaluating to Y when bool(X)==bool(Y)==False, e.g. 0 or [] == [] [] or 0 == 0 Admittedly there is an aestheically unpleasing asymmetry here. But it means you could write code such as Z = X xor Y if Z: <do something with Z> # Z is known to be either X or Y Of course, there is no evaluation short-circuiting. Rob Cliffe ----- Original Message ----- From: "Robert Kern" <robert.kern@gmail.com> To: <python-ideas@python.org> Sent: Tuesday, October 27, 2009 11:18 PM Subject: Re: [Python-ideas] XOR
On 2009-10-27 18:07 PM, geremy condra wrote:
On Tue, Oct 27, 2009 at 6:46 PM, Georg Brandl<g.brandl@gmx.net> wrote:
Sturla Molden schrieb:
Why does Python have a bitwise but not a logical xor operator?
How often do you need the xor operator?
1) Technically, an operator is *never* needed, as its just syntactic sugar. 2) It sure would make crypto code look prettier, as we rely on xor operations extensively.
No, it wouldn't. Crypto uses the bitwise xor which we already have an operator for: ^.
As I stated in the referenced thread, to me, the most compelling reason there is no "xor" keyword to go with "and" and "or" is that one cannot make an xor that shares the same short-circuiting behavior. Or the behavior of returning one of the operand objects rather than a coerced bool. Without either of those behaviors, there is little benefit to having a keyword operator where a trivial one-liner will suffice.
-- Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
_______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
Sturla Molden skrev:
Why does Python have a bitwise but not a logical xor operator? It's even weirder because boolean objects do have a __xor__ method.
True.__xor__(False) True True xor False SyntaxError: invalid syntax
Doesn't this look like a case for new syntax before the moratorium? S.M.
On Tue, Oct 27, 2009 at 4:46 PM, Sturla Molden <sturla@molden.no> wrote:
Sturla Molden skrev:
Why does Python have a bitwise but not a logical xor operator? It's even weirder because boolean objects do have a __xor__ method.
True.__xor__(False) True True xor False SyntaxError: invalid syntax
Doesn't this look like a case for new syntax before the moratorium?
No the proper syntax is
False ^ False False False ^ True True True ^ False True True ^ True False
On Tue, Oct 27, 2009 at 5:13 PM, Sturla Molden <sturla@molden.no> wrote:
I think you are missing the point: ^ is bitwise or. Don't think we can always go from bitwise to boolean operator by casting to bool. Xor is a special case.
No you cannot always go from bitwise to logical, but in the xor case it works, which is the case you were wanting to add syntax for. Just because it is also bitwise syntax does not mean you cannot use it in logical operations where it is intended to work. The only benefit I see to adding xor is not having to cast items to a bool before doing the test, which could be solved with no syntax changes by adding the __xor__ method to the types you want to check, though I think adding this to builtins would fall under the moratorium. could define __xor__ for all objects, except where the bitwise xor is intended as def __xor__(self, other): return bool(self) ^ bool(other)
Btw,
from operator import xor help(xor) Help on built-in function xor in module operator:
xor(...) xor(a, b) -- Same as a ^ b.
And just nosing around for code that uses the word xor you guys are talking about reserving: as a function (these are not examples that just do ^): http://www.google.com/codesearch/p?hl=en&sa=N&cd=4&ct=rc#y7s_SD3fSG4/Python-1.2/Demo/sgi/gl/glstdwin/glstdwin.py&q=xor%20file:%5C.py$&l=376 http://www.google.com/codesearch/p?hl=en&sa=N&cd=10&ct=rc#xFWT5g5T9_Y/Singular-3-0-2/Singular/cnf2ideal.py&q=xor%20file:%5C.py$&l=68 as a variable name: http://www.google.com/codesearch/p?hl=en&sa=N&cd=5&ct=rc#OmjbpIVasQU/Zope-3.2.0b1/Dependencies/twisted-Zope-3.2.0b1/twisted/words/protocols/toc.py&q=xor%20file:%5C.py$&l=71 http://www.google.com/codesearch/p?hl=en&sa=N&cd=20&ct=rc#ptHG4K-HqzI/lib/python2.4/site-packages/mixminion/test.py&q=xor%20file:%5C.py$&l=861 --yuv
On Tue, Oct 27, 2009 at 3:46 PM, Sturla Molden <sturla@molden.no> wrote:
Doesn't this look like a case for new syntax before the moratorium?
No, the moratorium (assuming it's accepted) starts retroactively the day 3.1 was released. -- --Guido van Rossum (python.org/~guido)
On Tue, Oct 27, 2009 at 3:41 PM, Sturla Molden <sturla@molden.no> wrote:
Why does Python have a bitwise but not a logical xor operator? It's even weirder because boolean objects do have a __xor__ method.
It would be nice for symmetry, but it would be infrequently used compared to the other operators and is not strictly necessary as logical XOR cannot and would not short-circuit, unlike logical AND and OR. Cheers, Chris -- http://blog.rebertia.com
-1 It would be unnice in my opinion because it would falsely suggest some kind of parallelism with how 'and' and 'or' work. Just use bool(x) ^ bool(y) if you really want xor behavior:
bool(3) ^ bool(4) False bool(3) ^ bool(None) True
--- Bruce http://www.vroospeak.com On Tue, Oct 27, 2009 at 3:48 PM, Chris Rebert <pyideas@rebertia.com> wrote:
On Tue, Oct 27, 2009 at 3:41 PM, Sturla Molden <sturla@molden.no> wrote:
Why does Python have a bitwise but not a logical xor operator? It's even weirder because boolean objects do have a __xor__ method.
It would be nice for symmetry, but it would be infrequently used compared to the other operators and is not strictly necessary as logical XOR cannot and would not short-circuit, unlike logical AND and OR.
Cheers, Chris -- http://blog.rebertia.com _______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
On Tue, Oct 27, 2009 at 10:41 PM, Sturla Molden <sturla@molden.no> wrote:
Why does Python have a bitwise but not a logical xor operator? It's even weirder because boolean objects do have a __xor__ method.
Have you looked at the recent python-list thread starting at: http://mail.python.org/pipermail/python-list/2009-July/188589.html ? Mark
On Tue, Oct 27, 2009 at 6:41 PM, Sturla Molden <sturla@molden.no> wrote:
Why does Python have a bitwise but not a logical xor operator?
.. because it does: !=
True != True False True != False True False != False False
In 2.x you can even use <> if you like syntactic sugar. :-) On arbitrary types a xor b is arguably bool(a) != bool(b) rather than simple a != b, but it is rare enough to warrant additional syntax. I thought I've seen this answered in an FAQ list somewhere.
Alexander Belopolsky wrote:
On Tue, Oct 27, 2009 at 6:41 PM, Sturla Molden <sturla@molden.no> wrote:
Why does Python have a bitwise but not a logical xor operator?
.. because it does: !=
True != True False True != False True False != False False
In 2.x you can even use <> if you like syntactic sugar. :-)
On arbitrary types a xor b is arguably bool(a) != bool(b) rather than simple a != b, but it is rare enough to warrant additional syntax.
I thought I've seen this answered in an FAQ list somewhere.
I've seen this in Java. But the field is different there, with no operator overloading != is always be equivalent with logical XOR. I'm +0 on the proposal, for being very rarely needed.
participants (13)
-
Alexander Belopolsky
-
Bruce Leban
-
Chris Rebert
-
Dj Gilcrease
-
Georg Brandl
-
geremy condra
-
Guido van Rossum
-
Lie Ryan
-
Mark Dickinson
-
Rob Cliffe
-
Robert Kern
-
Sturla Molden
-
Yuvgoog Greenle