我正在阅读以下文章:
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/