相关疑难解决方法(0)

为什么([1,0] == True中的1)评估为False?

当我看到这个问题的答案时,我发现我不明白自己的答案.

我真的不明白这是如何被解析的.为什么第二个示例返回False?

>>> 1 in [1,0]             # This is expected
True
>>> 1 in [1,0] == True     # This is strange
False
>>> (1 in [1,0]) == True   # This is what I wanted it to be
True
>>> 1 in ([1,0] == True)   # But it's not just a precedence issue!
                           # It did not raise an exception on the second example.

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    1 in ([1,0] == …
Run Code Online (Sandbox Code Playgroud)

python syntax operator-precedence

149
推荐指数
1
解决办法
4630
查看次数

为什么表达式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
查看次数

为什么`True是False == False`,在Python中是假的?

为什么在使用括号时这些语句按预期工作:

>>> (True is False) == False
True

>>> True is (False == False)
True
Run Code Online (Sandbox Code Playgroud)

但是False当没有括号时它会返回?

>>> True is False == False
False
Run Code Online (Sandbox Code Playgroud)

python python-3.x

29
推荐指数
3
解决办法
2705
查看次数

标签 统计

python ×3

operator-precedence ×1

python-3.x ×1

syntax ×1