大于256且小于-5的整数缓存 [英] Integer caching for numbers greater than 256 and less than -5
问题描述
我知道python有一个小整数的概念,即从 -5
到 256
的数字,如果两个变量在此范围内分配相同的数字,它们都将使用相同的基础对象。
I know that python has a concept of small integers which are numbers from -5
to 256
, and if two variables assign to same numbers between this range, they both will use the same underlying object.
摘自Python文档,
#ifndef NSMALLPOSINTS
#define NSMALLPOSINTS 257
#endif
#ifndef NSMALLNEGINTS
#define NSMALLNEGINTS 5
#endif
/ *小整数预先分配在该数组,以便可以共享
。预分配的整数是在
范围-NSMALLNEGINTS(包括)到NSMALLPOSINTS(不包括)中的整数。
* /
/* Small integers are preallocated in this array so that they can be shared. The integers that are preallocated are those in the range -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive). */
当前实现保留了一个整数对象数组对于-5到256之间的所有
整数,当您在该范围内创建一个int时,
实际上只是返回对现有对象的引用。因此
应该可以更改1的值。我怀疑这种情况下Python的
行为是未定义的。 :-)
The current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you actually just get back a reference to the existing object. So it should be possible to change the value of 1. I suspect the behaviour of Python in this case is undefined. :-)
示例,
a = 255
b = 255
print(id(a))
print(id(b))
提供相同的ID,
1561854394096
1561854394096
这很有意义,并在此答案中作了解释, is表示运算符对整数的行为异常
Which makes sense and also explained on this answer, "is" operator behaves unexpectedly with integers
如果两个数字小于 -5
,则它们也应具有如下所示的不同ID,
If two numbers are less than -5
, they should also have different IDs as follows,
a = -6
b = -6
print(id(a))
print(id(b))
给出,
2827426032208
2827426032272
到目前为止,这是有道理的,
this makes sense so far,
但是任何大于 256
的数字都应具有不同的 id
,
But any number greater than 256
should have different id
,
这应该返回不同的ID,
This should return different IDs,
a = 257
b = 257
print(id(a))
print(id(b))
但不是
2177675280112
2177675280112
即使我使用非常大的整数,ID也是一样的
Even when I am using very large integer, the IDs are same,
a = 2571299123876321621378
b = 2571299123876321621378
print(id(a))
print(id(b))
gi ves me,
gives me,
1956826139184
1956826139184
有人可以告诉我为什么大于256的数字具有相同的ID,即使在Python代码中范围是 -5
到 257
(不包括)
Can someone tell me why number greater than 256 have same IDs even though in the Python code the range is -5
to 257
(not inclusive)
编辑:
我尝试将PyCharm与Python 2.7和3.6一起使用。还尝试过PythonTutor.com
I have tried using PyCharm with both Python 2.7 and 3.6. Also tried on PythonTutor.com
推荐答案
在薄荷Python 3.6.3(以及2)上我无法重现。我的猜测是 PyCharm
或pythontutor在解释之前将运行结果包装起来-由于这些不是开放代码,我们看不到内部结构,因此无法验证。我认为这是对的,这是因为(以下所有内容都是薄荷Python 3):
On mint Python 3.6.3 (2 as well) I cannot reproduce. My guess is PyCharm
or pythontutor are wrapping the run in something before interpreting - since those are not open code we cannot see the internals so I cannot verify. The reason I think this is true, is while (everything below is mint Python 3):
>>> x=2571299123876321621378
>>> y=2571299123876321621378
>>> print(id(x),id(y))
140671727739528 140671727739808
您可以拥有
>>> def bla():
... x=2571299123876321621378
... y=2571299123876321621378
... id(x)
... print(id(x),id(y))
...
>>> bla()
140671727742528 140671727742528
因此将两个整数包装在解释器可以编译的内容中对于这些额外的优化-就像对两个定义使用相同的常量。请注意,这也是受限制的:
so wrapping the two integers in something the interpreter can compile allows for these extra optimizations - like using the same constant for both definitions. Note this is limited as well:
>>> def bla():
... x=2571299123876321621378
... y=2571299123876321621378
... print(id(x),id(y))
... x+=1
... y+=1
... print(id(x),id(y))
...
>>> bla()
140671727755592 140671727755592
140671728111088 140671728108808
我不会有这样的代码取决于这一点-保证金只在-5到256之间。
I would not have code that depends on this on any way - the guarantee is only on -5 to 256.
这篇关于大于256且小于-5的整数缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!