为啥Python有相同的不可变对象id不同

\u0026lt;Python源码剖析\u0026gt;上解释的很清楚:Python里一切都是对象.所以1,2,3,4...这些整数也都是对象.这些基本的不可变对象在python里会被频繁的引用,创建,如果不找到好的办法的话很容易让python引发效率瓶颈,所以python引入了整数对象池的机制.intobject.c中代码片段#ifndef NSMALLPOSINTS#define NSMALLPOSINTS 257#endif#ifndef NSMALLNEGINTS#define NSMALLNEGINTS 5#endif#if NSMALLNEGINTS + NSMALLPOSINTS \u0026gt; 0/* References to small integers are saved in this array so that they can be shared. The integers that are saved are those in the range -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).*/static PyIntObject *small_ints;#endif 上面清楚写了=?修改索引为i的字符,而可变对象支持修改。


    推荐阅读