Message246619
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. |
|
Date |
User |
Action |
Args |
2015-07-12 00:11:54 | martin.panter | set | recipients:
+ martin.panter, scoder, benjamin.peterson, mrabarnett, steven.daprano, serhiy.storchaka, candide |
2015-07-12 00:11:54 | martin.panter | set | messageid: <1436659914.58.0.529536947591.issue24612@psf.upfronthosting.co.za> |
2015-07-12 00:11:54 | martin.panter | link | issue24612 messages |
2015-07-12 00:11:53 | martin.panter | create | |
|