简体   繁体   English

为什么(不是None)== True评估为True?

[英]Why does (not None) == True evaluate to True?

This tripped me up recently in a piece of Python code - I was checking if any of the values in a list were False, so I used something like: 最近这用一段Python代码使我绊倒了-我正在检查列表中的任何值是否为False,所以我使用了类似的方法:

if any(not v for v in values):
    # Do something...

However, I forgot that there were None values in the list, so I kept getting confusing outputs until I remembered to check for that first. 但是,我忘记了列表中None值,所以一直使输出变得混乱,直到我记得先检查一下。 That got me wondering though - what's the rationale behind (not None) == True ? 这让我感到纳闷-背后的原理是什么(not None) == True None == False evaluates to False , so it's not reflexive, and it's not immediately clear to me why the logical negation of the None object should evaluate to True . None == False计算结果为False ,因此它不是自反的,并且我现在还不清楚为什么None对象的逻辑取反应该计算为True

not is an operator that produces a boolean value, the inverse of the truth value of it's operand. not产生布尔值的运算符, 布尔 是其操作数的真值的倒数。

I suspect you are confusing the truth value with the boolean value. 我怀疑您是将真实值与布尔值混淆了。 The latter is only either True or False , but the truth value of any Python object can be converted to a boolean value by passing it to the bool() function. 后者只是TrueFalse ,但是任何Python对象的True值都可以通过将其传递给bool()函数而转换为布尔值。 Statements like if and while , operators like and , or and not test for truth values, not specifically boolean values. 诸如ifwhile语句,运算符和and or and的语句not测试真值,而不是布尔值。 any() and all() test each value in the iterable for truth as well. any()all()测试迭代器中的每个值是否为真。

As such, you should never need to do if some_expression == True or if not some_expression == True when testing truth values. 因此,在测试真值时,无需执行if some_expression == Trueif not some_expression == True事情。 Just use if some_expression or if not some_expression . 只需使用if some_expressionif not some_expression使用if some_expression if not some_expression

Note that not has to return a boolean value because you can't, usually, invert a truth value. 请注意, not 必须返回一个布尔值,因为你不能,通常反转真值。 What would the inverse be of 0 or an empty list, for example? 例如,倒数是0还是一个空列表? 0 is considered false, but any other integer would be considered true, so what value would not 0 return if not False ? 0被认为是错误的,但是任何其他整数都将被认为是正确的,因此如果不是False则不会返回not 0什么值? For lists, what would you put in the list to produce not [] ? 对于列表,您将在列表中放入什么not []产生not []

The explanation for why it happens is in the comments to your question. 发生问题的原因在您问题的注释中。 A good way to check if anything within your list evaluates to false is: 检查列表中是否有错误的好方法是:

if not all(values):

There are many ways to do what you're doing but this for me is more readable than the list comprehension you have. 有很多方法可以做您正在做的事情,但是对我来说,这比您拥有的列表理解更具可读性。

This answer to a related question contains some good info you may want to check out, explaining detail about the use of True , False , and None in Python: https://stackoverflow.com/a/9494887/4174975 此相关问题的答案包含一些您可能想查看的有用信息,解释了有关在Python中使用TrueFalseNone详细信息: https : //stackoverflow.com/a/9494887/4174975

An explanation that's perhaps more specific to your answer can be found in the O'Reilly book Introducing Python by Bill Lubanovic, who refers to Python programs that use the concept of "truthiness" and "falsiness" to check for empty data structures as well as False conditions. 比尔·卢巴诺维奇(Bill Lubanovic)在O'Reilly的《 Python简介》一书中找到了可能更适合您答案的解释,该书指的是使用“真实性”和“虚假性”的概念检查空数据结构的Python程序以及False条件。 The author even has a subsection on page 88 titled "None is Useful" which elaborates: 作者甚至在第88页有一个标题为“无用”的小节,其中详细介绍了:

None is a special Python value that holds a place when there is nothing to say. None一个是特殊的Python值,当没有其他要说的内容时,它会占有一席之地。 It is not the same as the boolean value False , although it looks false when evaluated as boolean. 它与布尔值False ,尽管在评估为布尔值时看起来为false。 [...] You'll need None to distinguish a missing value from an empty value... zero-valued integers or floats, empty string (''), lists([]), tuples((,)), dictionaries({}), and sets(set()) are all False , but are not equal to None . [...]您将需要None来将缺失值与空值区分开...零值整数或浮点数,空字符串(''),列表([]),元组((,)),字典({})和sets(set())均为False ,但不等于None

So that is the rationale behind why None behaves the way that it does, and has subtle qualities apart from what you get with True and False or a 1 or a 0 . 因此,这就是为什么 None表现出其行为方式的理由,并且除了TrueFalse10之外,还具有微妙的品质。

As to the other part of your question, it appears to be a logical tautology: None is False because Python interprets the None as being the equivalent of a null for the reasons found above. 至于问题的另一部分,这似乎是一种逻辑上的重言式: NoneFalse因为由于上述原因,Python将None解释为等同于null By adding not to a None , you are inverting its boolean value, which turns it into a True . 通过not添加None ,您正在反转其布尔值,这使其变为True

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

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