简体   繁体   English

为什么 bool([]) == False 而 [] == False 是 False 而不是 Python 布尔逻辑中的 True

[英]Why is bool([]) == False while [] == False is False and not True in python boolean logic

I've just started learning Python and i was trying this我刚刚开始学习 Python,我正在尝试这个

[] == False #False 

but :但 :

bool([]) #False

from what i got values like [],0 .. are False what did i missed exactly and thanks!从我得到的值,如 [],0 .. 是False我到底错过了什么,谢谢!

[] != False ,但bool([]) == bool(False)

The operator == is very literal.运算符==非常字面。 If the 2 things you are comparing are not exactly the same (this includes types, like "2" == 2 is False ) then the result will always be False .如果您要比较的两件事不完全相同(这包括类型,例如"2" == 2 is False ),那么结果将始终为False So the boolean False is not literally the same thing as an empty list [] which is why [] == False is False .所以布尔值False与空列表[]的字面意思不同,这就是为什么[] == FalseFalse的原因。

An empty list is just treated as "False" when converted to a boolean, which you did with bool([]) .当转换为布尔值时,空列表仅被视为“False”,您使用bool([])进行了此操作。 So the output of bool([]) is False which is literally the same as False .所以bool([])的输出是False ,这实际上与False相同。 Thus bool([]) == False is True .因此bool([]) == FalseTrue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM