Python如何为文字分配内存地址?文字的内存地址
>>> id(1)
4298171352
>>> id(2)
4298171328
>>> id(1.0019)
4299162496
>>> id(1.0025)
4299162496
>>> id(1.00999)
4299162496
>>> a = 1.0025
>>> b = 1.00999
>>> id(a)
4299162496
>>> id(b)
4299162472
>>>
[email protected] ~ $ python
Python 2.7.10 |Anaconda 2.2.0 (x86_64)| (default, May 28 2015, 17:04:42)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>> id(1)
4298171352
它是如何,1.0025和1.0999对应于相同的存储器位置,但是,当我设置a和b等于那些现在有不同的存储位置的相应值?当我重新启动Python时,它的内存地址是一样的吗?
此外,这是否意味着每个整数和分箱浮点数和字符都有一个预先存在的内存地址?
由于您没有在任何位置分配值,因此可以重新使用内存。 – Barmar
Python缓存小整数(我现在不记得确切的范围),并且由于缓存是在启动时完成的,从一次运行到下一次运行它们具有相同的内存地址似乎是合理的 - 但是,没有关于python内存分配器如何工作的专家(以及它如何与操作系统的内存分配器交互)。 – mgilson
另请参阅:http://stackoverflow.com/q/306313/748858 – mgilson