Martin Panter added the comment:

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.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue24612>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to