python return not & and 是什么意思?

标签 python

我正在阅读以下文章:

http://stefaanlippens.net/python-asynchronous-subprocess-pipe-reading

我不确定以下函数返回什么:

def eof(self):
    '''Check whether there is no more content to expect.'''
    return not self.is_alive() and self._queue.empty()

查看下面的问题:

python and / or operators return value

x and y, if x is false, then x, else y 

在这种情况下 x 是什么?是吗 self.is_alive() 或者 不是 self.is_alive()

最佳答案

对于没有括号的表达式,Python 使用了一个名为 operator precedence 的概念以确定首先要评估的内容。

not 运算符的优先级高于 and 运算符,因此您的表达式被解析为:

    (not self.is_alive())
and 
    self._queue.empty()

self.is_alive() 首先被调用。如果返回 True,则 not 表达式生成 False,而 self._queue.empty() 表达式为甚至没有运行。返回 not self.is_alive()False 结果。毕竟, 表达式无法在表达式左侧已确定为假的情况下变为真。

另一方面,如果 self.is_alive() 返回 False,则 not False 变为 True,并且self._queue.empty() 的结果从整个表达式返回。

因此,如果 self._queue.empty() 函数返回 bool 值,则 eof() 方法将返回 False对象仍然存在队列不为空。仅当对象不再存在且队列为空时,它才会返回 True。

关于python return not & and 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32699450/

相关文章:

python - 滚动条滚动文本小部件,使用网格布局,在 Tkinter

python - 出现错误 : TypeError: object of type 'float' has no len() in pandas

python - 为什么 [].append(1) 是 None

python - 如何访问更改状态的按钮

python - Pandas 根据索引添加 NA 行

python - 如何获取 urwid RadioButton 和 CheckBox 值?

python - Numpy - 归一化 RGB 像素阵列

python - 如何打印类属性/元素的文档字符串?

python - OS.STAT().ST_SIZE 在 Python 中给出了不正确的大小

python - MySQL:pymysql 或 mysqldb 访问字典游标功能