我相对来说是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
请问有人能给我解释一下吗?这些语句是如何在内部进行评估的?我觉得这可能与链接比较有关(例如:见此处),但我真的不明白这是否正确以及原因。