问题:False == 0和True == 1是实现细节还是由语言保证?

是否可以保证False == 0True == 1,在Python中(假设用户没有重新分配它们)?例如,是否以任何方式保证以下代码将始终产生相同的结果,而不管Python的版本如何(既有现有版本,也可能是未来版本)?

0 == False  # True
1 == True   # True
['zero', 'one'][False]  # is 'zero'

任何对官方文档的引用将不胜感激!

编辑:如许多答案所述,bool继承自int。因此,可以将问题改写为:“文档是否正式声明程序员可以依赖从整数(带有值01?)继承的布尔 ”。这个问题与编写不会因为实现细节而失败的健壮代码有关!

Is it guaranteed that False == 0 and True == 1, in Python (assuming that they are not reassigned by the user)? For instance, is it in any way guaranteed that the following code will always produce the same results, whatever the version of Python (both existing and, likely, future ones)?

0 == False  # True
1 == True   # True
['zero', 'one'][False]  # is 'zero'

Any reference to the official documentation would be much appreciated!

Edit: As noted in many answers, bool inherits from int. The question can therefore be recast as: “Does the documentation officially say that programmers can rely on booleans inheriting from integers, with the values 0 and 1?”. This question is relevant for writing robust code that won’t fail because of implementation details!


回答 0

在Python 2.x的,这是没有保证的,因为它是可能的True,并False重新分配。但是,即使发生这种情况,仍然正确返回布尔True和布尔False进行比较。

在Python 3.x中,TrueFalse是关键字,并且始终等于10

通常情况下,在Python 2中,并且始终在Python 3中:

False对象的类型bool是的子类int

object
   |
 int
   |
 bool

这是在您的示例['zero', 'one'][False]中起作用的唯一原因。它不适用于不是整数子类的对象,因为列表索引仅适用于整数或定义__index__方法的对象(感谢mark-dickinson)。

编辑:

当前的python版本和Python 3都是如此。python 2.6文档和python 3文档都说:

整数有两种类型:[…]整数(int)[…]布尔值(bool)

在布尔小节中:

布尔值:它们表示真值False和True布尔值在几乎所有上下文中的行为分别类似于值0和1,但exceptions是当转换为字符串时,字符串“ False”或“ True”分别返回。

对于Python 2也有:

在数字上下文中(例如,用作算术运算符的参数时),它们的[False和True]分别类似于整数0和1。

因此,布尔值在Python 2.6和3中被明确视为整数。

因此,在Python 4出现之前,您是安全的。;-)

In Python 2.x this is not guaranteed as it is possible for True and False to be reassigned. However, even if this happens, boolean True and boolean False are still properly returned for comparisons.

In Python 3.x True and False are keywords and will always be equal to 1 and 0.

Under normal circumstances in Python 2, and always in Python 3:

False object is of type bool which is a subclass of int:

object
   |
 int
   |
 bool

It is the only reason why in your example, ['zero', 'one'][False] does work. It would not work with an object which is not a subclass of integer, because list indexing only works with integers, or objects that define a __index__ method (thanks mark-dickinson).

Edit:

It is true of the current python version, and of that of Python 3. The docs for python 2.6 and the docs for Python 3 both say:

There are two types of integers: […] Integers (int) […] Booleans (bool)

and in the boolean subsection:

Booleans: These represent the truth values False and True […] Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings “False” or “True” are returned, respectively.

There is also, for Python 2:

In numeric contexts (for example when used as the argument to an arithmetic operator), they [False and True] behave like the integers 0 and 1, respectively.

So booleans are explicitly considered as integers in Python 2.6 and 3.

So you’re safe until Python 4 comes along. ;-)


回答 1

链接到PEP讨论Python 2.3中的新布尔类型:http : //www.python.org/dev/peps/pep-0285/

将布尔值转换为int时,整数值始终为0或1,但是将int值转换为bool时,除0以外的所有整数的布尔值均为True。

>>> int(False)
0
>>> int(True)
1
>>> bool(5)
True
>>> bool(-5)
True
>>> bool(0)
False

Link to the PEP discussing the new bool type in Python 2.3: http://www.python.org/dev/peps/pep-0285/.

When converting a bool to an int, the integer value is always 0 or 1, but when converting an int to a bool, the boolean value is True for all integers except 0.

>>> int(False)
0
>>> int(True)
1
>>> bool(5)
True
>>> bool(-5)
True
>>> bool(0)
False

回答 2

在Python 2.x中,根本无法保证:

>>> False = 5
>>> 0 == False
False

因此它可能会改变。在Python 3.x中,True,False和None是保留字,因此上面的代码不起作用。

通常,使用布尔值时,您应该假设False始终具有0的整数值(只要您不进行上述更改即可),True可以具有任何其他值。我不一定要依靠True==1,但是在Python 3.x上,无论如何,情况总是如此。

In Python 2.x, it is not guaranteed at all:

>>> False = 5
>>> 0 == False
False

So it could change. In Python 3.x, True, False, and None are reserved words, so the above code would not work.

In general, with booleans you should assume that while False will always have an integer value of 0 (so long as you don’t change it, as above), True could have any other value. I wouldn’t necessarily rely on any guarantee that True==1, but on Python 3.x, this will always be the case, no matter what.


回答 3

很简单。由于布尔与将整数评估为布尔有关,因此只有0给出错误的答案。所有非零值,浮点数,整数(包括负数)或您拥有的所有值都将返回true。

一个为什么有用的很好的例子是确定设备的电源状态。On是任何非零值,off是零。从电子上讲,这是有道理的。

若要相对确定值之间的正确或错误,您必须将其与之进行比较。这适用于字符串和数值,使用==!=<> >=<=等。

您可以为变量分配一个整数,然后根据该变量值获得true或false。

Very simple. As bool relates to evaluating an integer as a bool, ONLY zero gives a false answer. ALL Non-Zero values, floats, integers, including negative numbers, or what have you, will return true.

A nice example of why this is useful is determining the power status of a device. On is any non-zero value, off is zero. Electronically speaking this makes sense.

To determine true or false relatively between values, you must have something to compare it to. This applies to strings and number values, using == or != or <, > >=, <=, etc.

You can assign an integer to a variable and then get true or false based on that variable value.


回答 4

只需编写int(False),您将得到0,如果键入int(True)它将输出1

Just write int(False) and you will get 0, if you type int(True) it will output 1


回答 5

假是布尔。它具有不同的类型。它是不同于整数的0的对象。

0 == False因为将False强制转换为整数,所以返回True。int(False)返回0

==运算符的python文档说(help(’==’)):

运营商<>==>=<=,和!=比较两个对象的值。这些对象不必具有相同的类型。如果两者都是数字,则它们将转换为通用类型。

结果,需要比较时将False转换为整数。但它不同于0。

>>> 0 is False
False

False is a bool. It has a different type. It is a different object from 0 which is an integer.

0 == False returns True because False is cast to an integer. int(False) returns 0

The python documentation of the == operator says (help(‘==’)):

The operators <, >, ==, >=, <=, and != compare the values of two objects. The objects need not have the same type. If both are numbers, they are converted to a common type.

As a consequence False is converted to an integer for the need of the comparison. But it is different from 0.

>>> 0 is False
False

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。