1
echo1937 2015-07-17 17:59:54 +08:00
学python的时候没有接触"可变对象""不可变对象""引用计数"这样的概念吗?
你看的都什么出版社的书啊. |
2
aragakiiyui OP @echo1937 数字是不可变对象吧,而且引用计数不是针对可变对象么?!
|
4
11 2015-07-17 18:07:58 +08:00 1
@aragakiiyui 回答你的问题:
https://docs.python.org/2/c-api/int.html#c.PyInt_FromLong 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. :-) |
5
bearzk 2015-07-17 18:09:23 +08:00
https://docs.python.org/2/c-api/int.html
``` PyObject* PyInt_FromLong(long ival) Return value: New reference. Create a new integer object with a value of ival. 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. :-) ``` 文档说是-5 ~ 256都是存好的, 可能是为了快吧. |
6
lzjun 2015-07-17 18:09:41 +08:00
[-5, 256]这些小对象由于使用频率高,python把他们缓存在内存中
|
7
aragakiiyui OP |
8
luobuda 2015-07-17 21:03:53 +08:00
Cpython代码
#ifndef NSMALLPOSINTS #define NSMALLPOSINTS 257 #endif #ifndef NSMALLNEGINTS #define NSMALLNEGINTS 5 #endif #if NSMALLNEGINTS + NSMALLPOSINTS > 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[NSMALLNEGINTS + NSMALLPOSINTS]; #endif small_ints就是小整数缓存 05~256 可以修改重新编译 |
9
BUPTGuo 2015-07-17 21:09:21 +08:00
Java也有类似的策略,恩
|