False == 0 和 True == 1 是实现细节还是由语言保证? [英] Is False == 0 and True == 1 an implementation detail or is it guaranteed by the language?
问题描述
在 Python 中是否保证 False == 0
和 True == 1
(假设它们不是由用户重新分配的)?例如,是否以任何方式保证以下代码将始终产生相同的结果,无论 Python 版本如何(现有的和可能的未来版本)?
0 == False # True1 == 真 # 真['零', '一'][假] # 是 '零'
对官方文档的任何参考将不胜感激!
编辑:正如许多答案所指出的,bool
继承自 int
.因此,问题可以改写为:文档是否正式说程序员可以依赖从整数继承的布尔值,值为 0
和 1
?".这个问题与编写不会因实现细节而失败的健壮代码相关!
在 Python 2.x 中,这是不保证的,因为 True
和 是可能的False
要重新分配.但是,即使发生这种情况,仍会正确返回 boolean True 和 boolean False 进行比较.
在 Python 3.x 中 True
和 False
是关键字,它们总是等于 1
和 0
.
通常情况下在 Python 2 中,始终在 Python 3 中:
False
对象属于 bool
类型,它是 int
的子类:
对象|整数|布尔值
这是在您的示例中 ['zero', 'one'][False]
起作用的唯一原因.它不适用于不是整数子类的对象,因为列表索引仅适用于整数或定义 __index__
方法(感谢 mark-dickinson).
当前的 Python 版本和 Python 3 都是如此.python 2 文档 和 Python 3 文档 都说:
<块引用>有两种类型的整数:[...] Integers (int) [...] Booleans (bool)
在布尔小节中:
<块引用>Booleans:这些代表真值 False 和 True [...] 布尔值的行为分别类似于值 0 和 1,在几乎所有上下文中,例外是当转换为字符串时,字符串False";或真"分别返回.
还有,适用于 Python 2::><块引用>
在数字上下文中(例如,当用作算术运算符的参数时),它们 [False 和 True] 的行为分别类似于整数 0 和 1.
因此布尔值在 Python 2 和 3 中被明确视为整数.
所以在 Python 4 出现之前你是安全的.;-)
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!
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 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 and 3.
So you're safe until Python 4 comes along. ;-)
这篇关于False == 0 和 True == 1 是实现细节还是由语言保证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!