Skip to main content
 首页 » 编程设计

Python 运算符优先级

2025年05月04日16www_RR

我应该如何在 Python 中解释这句话(在运算符优先级方面)?

c = not a == 7 and b == 7 

作为 c = not (a == 7 and b == 7) 或 c = (not a) == 7 and b == 7?

谢谢

请您参考如下方法:

使用 dis 模块:

>>> import dis 
>>> def func(): 
...     c = not a == 7 and b == 7 
...      
>>> dis.dis(func) 
  2           0 LOAD_GLOBAL              0 (a) 
              3 LOAD_CONST               1 (7) 
              6 COMPARE_OP               2 (==) 
              9 UNARY_NOT            
             10 JUMP_IF_FALSE_OR_POP    22 
             13 LOAD_GLOBAL              1 (b) 
             16 LOAD_CONST               1 (7) 
             19 COMPARE_OP               2 (==) 
        >>   22 STORE_FAST               0 (c) 
             25 LOAD_CONST               0 (None) 
             28 RETURN_VALUE   

所以,它看起来像:

c = (not(a == 7)) and (b == 7)