为什么 (0-6) 是 -6 = False? [英] Why (0-6) is -6 = False?

查看:18
本文介绍了为什么 (0-6) 是 -6 = False?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

可能的重复:
Python是"运算符对整数的行为出乎意料

今天我尝试调试我的项目,经过几个小时的分析,我得到了这个:

<预><代码>>>>(0-6) 是 -6错误的

但是,

<预><代码>>>>(0-5) 是 -5真的

你能给我解释一下,为什么?也许这是某种错误或非常奇怪的行为.

>linux2 上的 Python 2.7.3(默认,2012 年 4 月 24 日,00:00:54)[GCC 4.7.0 20120414(预发布)]>>>类型(0-6)<输入'int'>>>>类型(-6)<输入'int'>>>>类型((0-6)是-6)<输入'bool'>>>>

解决方案

所有从 -5 到 256(含)的整数都缓存为与 CPython 共享相同地址的全局对象,因此 is 测试通过.

这个神器在http://www.laurentluce 中有详细解释.com/posts/python-integer-objects-implementation/,我们可以在 http://hg.python.org/cpython/file/tip/Objects/longobject.c.

<块引用>

一个特定的结构用于引用小整数并共享它们,因此访问速度很快.它是一个包含 262 个指向整数对象的指针的数组.这些整数对象是在我们上面看到的整数对象块中的初始化期间分配的.小整数范围从 -5 到 256.许多 Python 程序花费大量时间使用该范围内的整数,因此这是一个明智的决定.

这只是 CPython 的一个实现细节,您不应依赖于此. 例如,PyPy 实现了整数的 id 以返回自身,所以 (0-6) 是 -6 始终为真,即使它们在内部是不同的对象";它还允许您配置是否启用此整数缓存,甚至设置下限和上限.但一般来说,从不同来源检索的对象不会完全相同.如果要比较相等性,只需使用 ==.

Possible Duplicate:
Python “is” operator behaves unexpectedly with integers

Today I tried to debug my project and after a few hours of analysing I'd got this:

>>> (0-6) is -6
False

but,

>>> (0-5) is -5
True

Could you explain to me, why? Maybe this is some kind of bug or very strange behavior.

> Python 2.7.3 (default, Apr 24 2012, 00:00:54) [GCC 4.7.0 20120414 (prerelease)] on linux2
>>> type(0-6) 
<type 'int'>
>>> type(-6) 
<type 'int'>
>>> type((0-6) is -6)
<type 'bool'>
>>> 

解决方案

All integers from -5 to 256 inclusive are cached as global objects sharing the same address with CPython, thus the is test passes.

This artifact is explained in detail in http://www.laurentluce.com/posts/python-integer-objects-implementation/, and we could check the current source code in http://hg.python.org/cpython/file/tip/Objects/longobject.c.

A specific structure is used to refer small integers and share them so access is fast. It is an array of 262 pointers to integer objects. Those integer objects are allocated during initialization in a block of integer objects we saw above. The small integers range is from -5 to 256. Many Python programs spend a lot of time using integers in that range so this is a smart decision.

This is only an implementation detail of CPython and you shouldn't rely on this. For instance, PyPy implemented the id of integer to return itself, so (0-6) is -6 is always true even if they are "different objects" internally; it also allows you to configure whether to enable this integer caching, and even set the lower and upper bounds. But in general, objects retrieved from different origins will not be identical. If you want to compare equality, just use ==.

这篇关于为什么 (0-6) 是 -6 = False?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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