【发布时间】:2019-04-24 01:02:25
【问题描述】:
我对 Python 比较陌生,不了解以下行为:
为什么会这样声明
[] == False
评估为假,即使空列表是假的?
这里有更多示例 - 在许多其他情况下,空列表似乎确实以虚假的方式表现,只是不在 [] == False...
>>> 0 == False # what I'd expect
True
>>> not [] # what I'd expect
True
>>> bool([]) # what I'd expect
False
>>> [] == True # what I'd expect
False
>>> [] == False # unexpected
False
>>> bool([]) == False # why does it evaluate to True again now?
True
>>> ([]) == (bool([])) # unexpected
False
>>> (not []) == (not bool([]) # apparently adding 'not' makes it behave as expected again - why?
True
有人可以向我解释一下吗?这些陈述是如何内部评估的?
我感觉这可能与链接比较有关 (see e.g. here),但无法真正理解这是否正确以及原因。
【问题讨论】:
-
false 和 falsey 不是一回事