__ _ _ / _| ___ _ __(_) | | |_ / _ \| '__| | | | _| (_) | | | | | |_| \___/|_| |_|_|
在 Python 和 C++ 中,运算符优先级是不同的,有一些常用的语句返回的结果由于优先级的不同会有区别,可能会造成一些使用时的错误,比如:
py# python 1 & 3 == 1 # True
cpp// c++ bool result = 1 & 3 == 1; cout << result << endl; // 0
在 Python 中,& 的优先级比 == 高,所以先计算 1 & 3,结果为 1,然后再计算 1 == 1,结果为 True。
而在 C++ 中,== 的优先级比 & 高,所以先计算 3 == 1,结果为 False,然后再计算 1 & False,结果为 0。
在编译这段代码时, clang++ 也会给出警告:
shtest.cpp:6:21: warning: & has lower precedence than ==; == will be evaluated first [-Wparentheses] bool result = 1 & 3 == 1; ^~~~~~~~ test.cpp:6:21: note: place parentheses around the '==' expression to silence this warning bool result = 1 & 3 == 1; ^ ( ) test.cpp:6:21: note: place parentheses around the & expression to evaluate it first bool result = 1 & 3 == 1; ^ ( ) 1 warning generated.
接下来附上 Python 和 C++ 的运算符优先级表格,方便以后查阅。
以下表格总结了Python中的运算符优先级,从最高优先级(最具约束力)到最低优先级(最少约束力)。同一框中的运算符具有相同的优先级。除非明确给出语法,否则运算符都是二元的。同一框中的运算符从左到右分组(除了指数和条件表达式,它们从右到左分组)。
请注意,comparisons、membership tests 以及 identity tests 都具有相同的优先级,并具有从左到右的链接特性。
| Operator | Description |
|---|---|
(expressions...), [expressions...], {key: value...}, {expressions...} | Binding or parenthesized expression, list display, dictionary display, set display |
x[index], x[index:index], x(arguments...), x.attribute | Subscription, slicing, call, attribute reference |
await x | Await expression |
** | Exponentiation[^5] |
+x, -x, ~x | Positive, negative, bitwise NOT |
*, @, /, //, % | Multiplication, matrix multiplication, division, floor division, remainder[^6] |
+, - | Addition and subtraction |
<<, >> | Shifts |
& | Bitwise AND |
^ | Bitwise XOR |
| ` | ` |
in, not in, is, is not, <, <=, >, >=, !=, == | Comparisons, including membership tests and identity tests |
not x | Boolean NOT |
and | Boolean AND |
or | Boolean OR |
if – else | Conditional expression |
lambda | Lambda expression |
:= | Assignment expression |
| Precedence | Operator | Description | Associativity |
|---|---|---|---|
| 1 | :: | Scope resolution | Left-to-right → |
| 2 | a++ a-- | Suffix/postfix increment and decrement | Left-to-right → |
| 2 | <type>() <type>{} | Functional cast | |
| 2 | a() | Function call | |
| 2 | a[] | Subscript | |
| 2 | . -> | Member access | |
| 3 | ++a --a | Prefix increment and decrement | Right-to-left ← |
| 3 | +a -a | Unary plus and minus | |
| 3 | ! ~ | Logical NOT and bitwise NOT | |
| 3 | (<type>) | C-style cast | |
| 3 | *a | Indirection (dereference) | |
| 3 | &a | Address-of | |
| 3 | sizeof | Size-of [^1] | |
| 3 | co_await | await-expression (C++20) | |
| 3 | new new[] | Dynamic memory allocation | |
| 3 | delete delete[] | Dynamic memory deallocation | |
| 4 | .* ->* | Pointer-to-member | Left-to-right → |
| 5 | a*b a/b a%b | Multiplication, division, and remainder | |
| 6 | a+b a-b | Addition and subtraction | |
| 7 | << >> | Bitwise left shift and right shift | |
| 8 | <=> | Three-way comparison operator (since C++20) | |
| 9 | < <= > >= | For relational operators < and ≤ and > and ≥ respectively | |
| 10 | == != | For equality operators = and ≠ respectively | |
| 11 | a&b | Bitwise AND | |
| 12 | ^ | Bitwise XOR (exclusive or) | |
| 13 | ` | ` | Bitwise OR (inclusive or) |
| 14 | && | Logical AND | |
| 15 | ` | ` | |
| 16 | a?b:c | Ternary conditional [^2] | Right-to-left ← |
| 16 | throw | throw operator | |
| 16 | co_yield | yield-expression (C++20) | |
| 16 | = | Direct assignment (provided by default for C++ classes) | |
| 16 | += -= | Compound assignment by sum and difference | |
| 16 | *= /= %= | Compound assignment by product, quotient, and remainder | |
| 16 | <<= >>= | Compound assignment by bitwise left shift and right shift | |
| 16 | &= ^= ` | =` | Compound assignment by bitwise AND, XOR, and OR |
| 17 | , | Comma | Left-to-right → |