为False == 0和True == 1在Python实现细节抑或是由语言保证? [英] Is False == 0 and True == 1 in Python an implementation detail or is it guaranteed by the language?

查看:1061
本文介绍了为False == 0和True == 1在Python实现细节抑或是由语言保证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时能够保证所有的假== 0 真== 1 ,在Python?例如,它是在保证满足下列code总是会产生相同的结果,任何方式的Python(包括现有的和,可能的是,未来的)?的版本

Is it guaranteed that False == 0 and True == 1, in Python? 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'

官方文档的任何引用会更AP preciated!

Any reference to the official documentation would be much appreciated!

修改:正如许多答案指出,布尔 INT 继承。因此,问题可以被改写为:难道文档官方说,程序员可以依赖于布尔从整数继承,与值 0 1 ?。这个问题是相关的编写健壮code,因为实施细节不会失败!

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!

推荐答案

在Python 2.x的,这是的的保证,因为它有可能为重新分配。然而,即使出现这种情况,布尔True和布尔值false仍然是正确返回比较。

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.

在Python 3.x都有是关键字,将始终等于 1 0

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

在在Python 2正常情况下,始终在Python 3:

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

对象的类型是布尔 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).

编辑:

这是目前Python版本真实的,那Python的3​​ 文档为Python 2.6 和<一个href=\"http://docs.python.org/release/3.0.1/reference/datamodel.html#the-standard-type-hierarchy\">docs为Python 3 都说:

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:

有两种类型的整数:[...]整数(INT)[...]布尔(布尔)

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

在布尔小节:

布尔:这些重present真值假和真[...]布尔值分别表现得像值0和1,在几乎所有环境中,唯一的例外是,当转换为字符串时,字符串假或真返回,分别为。

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.

所以布尔被明确视为整数在Python 2.6和3。

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

所以你是安全的,直到怪蛇4走来。 ; - )

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

这篇关于为False == 0和True == 1在Python实现细节抑或是由语言保证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆