This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author martin.panter
Recipients benjamin.peterson, candide, martin.panter, mrabarnett, scoder, serhiy.storchaka, steven.daprano
Date 2015-07-12.00:11:53
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1436659914.58.0.529536947591.issue24612@psf.upfronthosting.co.za>
In-reply-to
Content
Funny, I ran into this one or two days ago, when refactoring some code that used the bitwise exclusive-or operator, since there is no boolean exclusive or:

-if (x == a) ^ (y != b): ...
+aa = x == a
+bb = y == b
+if aa ^ not bb: ...

It is fairly clear what I wanted to do above, but with “is not” you would have to avoid ambiguity:

>>> "spam" is not "ham"  # Using “is not” operator
True
>>> "spam" is (not "ham")  # Two distinct operators
False

I think it would be too complicated to make unary “not” bind more tightly than “and” in some cases but not in others. How would you handle these cases?

a + not b and c
a ** not b ** c
a is not not b

The way I see it, there is no equivalent problem with the other unary operators: arithmetic (+, -), bitwise (~), and “await”. Await has higher priority than all other binary operators, so no problem there. The other three are equal with the highest priority binary operator, exponentiation (**):

>>> 2 ** -1 ** 2  # Evaluated right to left
0.5
>>> 2 ** (-1) ** 2  # Negation first
2
>>> (2 ** -1) ** 2  # Left-hand exponentiation first
0.25

BTW, in the operator precedence table <https://docs.python.org/dev/reference/expressions.html#operator-precedence>, I think exponentiation should be in the same priority group as the arithmetic and bitwise unary operations. At the moment it says exponentiation has higher priority, but has a footnote saying this is reversed on the right-hand side of an exponentiation. This is unclear when applied to my example above.
History
Date User Action Args
2015-07-12 00:11:54martin.pantersetrecipients: + martin.panter, scoder, benjamin.peterson, mrabarnett, steven.daprano, serhiy.storchaka, candide
2015-07-12 00:11:54martin.pantersetmessageid: <1436659914.58.0.529536947591.issue24612@psf.upfronthosting.co.za>
2015-07-12 00:11:54martin.panterlinkissue24612 messages
2015-07-12 00:11:53martin.pantercreate