相关疑难解决方法(0)

为什么表达式0 <0 == 0在Python中返回False?

在Python 2.6中查看Queue.py,我发现这个构造我发现有点奇怪:

def full(self):
    """Return True if the queue is full, False otherwise
    (not reliable!)."""
    self.mutex.acquire()
    n = 0 < self.maxsize == self._qsize()
    self.mutex.release()
    return n
Run Code Online (Sandbox Code Playgroud)

如果maxsize为0,则队列永远不会满.

我的问题是它如何适用于这种情况?如何0 < 0 == 0被认为是假的?

>>> 0 < 0 == 0
False
>>> (0) < (0 == 0)
True
>>> (0 < 0) == 0
True
>>> 0 < (0 == 0)
True
Run Code Online (Sandbox Code Playgroud)

python

133
推荐指数
5
解决办法
6748
查看次数

python运算符的优先级和比较

以下比较产生True:

>>> '1' in '11'
True
>>> ('1' in '11') == True
True
Run Code Online (Sandbox Code Playgroud)

另一方面,使用括号,我得到一个TypeError:

>>> '1' in ('11' == True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument of type 'bool' is not iterable
Run Code Online (Sandbox Code Playgroud)

那么我怎么False没有括号?

>>> '1' in '11' == True
False
Run Code Online (Sandbox Code Playgroud)

python

7
推荐指数
2
解决办法
1687
查看次数

标签 统计

python ×2