EX.not knot

This commit is contained in:
暮晨 2018-11-16 22:38:53 +08:00 committed by 暮晨
parent 6ff9fd0842
commit 3ab485aca8

View File

@ -37,7 +37,7 @@ So, here we go...
- [> `is not ...` is not `is (not ...)`/`is not ...` 不是 `is (not ...)`](#-is-not--is-not-is-not-is-not--不是-is-not-)
- [> The surprising comma/意外的逗号](#-the-surprising-comma意外的逗号)
- [> Backslashes at the end of string/字符串末尾的反斜杠](#-backslashes-at-the-end-of-string字符串末尾的反斜杠)
- [> not knot!](#-not-knot)
- [> not knot!/别纠结!](#-not-knot别纠结)
- [> Half triple-quoted strings](#-half-triple-quoted-strings)
- [> Midnight time doesn't exist?](#-midnight-time-doesnt-exist)
- [> What's wrong with booleans?](#-whats-wrong-with-booleans)
@ -680,7 +680,7 @@ SyntaxError: EOL while scanning string literal
---
### > not knot!
### > not knot!/别纠结!
```py
x = True
@ -698,12 +698,12 @@ True
SyntaxError: invalid syntax
```
#### 💡 Explanation:
#### 💡 说明:
* Operator precedence affects how an expression is evaluated, and `==` operator has higher precedence than `not` operator in Python.
* So `not x == y` is equivalent to `not (x == y)` which is equivalent to `not (True == False)` finally evaluating to `True`.
* But `x == not y` raises a `SyntaxError` because it can be thought of being equivalent to `(x == not) y` and not `x == (not y)` which you might have expected at first sight.
* The parser expected the `not` token to be a part of the `not in` operator (because both `==` and `not in` operators have the same precedence), but after not being able to find an `in` token following the `not` token, it raises a `SyntaxError`.
* 运算符的优先级会影响表达式的求值顺序, 而在 Python 中 `==` 运算符的优先级要高于 `not` 运算符.
* 所以 `not x == y` 相当于 `not (x == y)`, 同时等价于 `not (True == False)`, 最后的运算结果就是 `True`.
* 之所以 `x == not y` 会抛一个 `SyntaxError` 异常, 是因为它会被认为等价于 `(x == not) y`, 而不是你一开始期望的 `x == (not y)`.
* 解释器期望 `not` 标记是 `not in` 操作符的一部分 (因为 `==``not in` 操作符具有相同的优先级), 但是它在 `not` 标记后面找不到 `in` 标记, 所以会抛出 `SyntaxError` 异常.
---