Python 运算符优先级 [英] Python operators precedence

查看:65
本文介绍了Python 运算符优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

as c = not (a == 7 and b == 7) 还是 c = (not a) == 7 and b == 7?>

谢谢

解决方案

使用 dis 模块:

<预><代码>>>>导入文件>>>定义函数():... c = 不是 a == 7 和 b == 7...>>>dis.dis(func)2 0 LOAD_GLOBAL 0 (a)3 LOAD_CONST 1 (7)6 COMPARE_OP 2 (==)9 UNARY_NOT10 JUMP_IF_FALSE_OR_POP 2213 LOAD_GLOBAL 1 (b)16 LOAD_CONST 1 (7)19 COMPARE_OP 2 (==)>>22 STORE_FAST 0 (c)25 LOAD_CONST 0(无)28 RETURN_VALUE

所以,它看起来像:

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

How should I interpret this sentence in Python (in terms of operators precedence)?

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

as c = not (a == 7 and b == 7) or c = (not a) == 7 and b == 7?

thanks

解决方案

Using dis module:

>>> 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  

So, it looks like:

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

这篇关于Python 运算符优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆