我了解可以测试清单是否为空,例如:
l = []
if not l:
print("empty")
但是我不明白为什么下一个代码不相等:
if l == False:
print("empty")
l
本身是list
类型,因此将其与布尔值进行比较将始终返回False
not l
是布尔表达式,因此True
有时会返回,具体取决于是否l
为空
>>>l = []
>>>type(l)
<type 'list'>
>>>l == True
False
>>>l == False
False
>>>
>>>
>>> type(not l)
<type 'bool'>
>>> (not l) == False
False
>>> (not l) == True
True
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句