Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python对象分配地址_Python_Python 2.7_Object_Memory_Allocation - Fatal编程技术网

Python对象分配地址

Python对象分配地址,python,python-2.7,object,memory,allocation,Python,Python 2.7,Object,Memory,Allocation,因此,我在Python2.7中遇到了一个有趣的行为,我很想知道为什么会发生这种情况。我在python 2.7中定义了以下4种数据结构: A设置为“st” 列表“lst” 元组“第三方物流” 字典为“dct” 在python中创建它们: st = set(['apple', 'orange', 'apple', 'pear', 'orange', 'banana']) lst = [1, 2, 3, "4"] tpl = (1, 2, 3, '4') dct = {'one': 1, '

因此,我在Python2.7中遇到了一个有趣的行为,我很想知道为什么会发生这种情况。我在python 2.7中定义了以下4种数据结构:

  • A设置为“st
  • 列表lst
  • 元组第三方物流
  • 字典为“dct
在python中创建它们:

st = set(['apple', 'orange', 'apple', 'pear', 'orange', 'banana']) 
lst = [1, 2, 3, "4"] 
tpl = (1, 2, 3, '4') 
dct = {'one': 1, 'two': 2, 'three': 3};
以下是4个对象的表示:

st.__repr__
<method-wrapper '__repr__' of set object at 0x7f234997ba48>

tpl.__repr__
<method-wrapper '__repr__' of tuple object at 0x7f23499667e0>

lst.__repr__
<method-wrapper '__repr__' of list object at 0x7f2349910b48>

dct.__repr__
<method-wrapper '__repr__' of dict object at 0x25af3a0>
st.\uu repr__
第三方物流__
一级报告__
分公司报告__
如您所见,与其他字典(0x7f23499******)相比,我的字典位于完全不同的内存部分(0x25af3a0)

我的问题是,为什么dictionary对象被放置到完全不同的内存区域?

Python版本是2.7.3(在linux上使用GCC4.7.2编译),linux版本是3.18.0-kali3-amd64#1 SMP Debian 3.18.6-1~kali2(2015-03-02)x86_64 GNU/linux。

Python使用了大量微小和临时对象(尤其是
dict
s,用于传递参数、存储属性等)。为了减少每次创建或销毁此类对象时分配和释放内存的开销,CPython通过为各种大小的小对象预先分配内存插槽阵列进行优化


因此,当您创建一个小对象时,Python不会按照您期望的方式分配内存。取而代之的是,它使用自己的启发式方法来选择一个已经留出的内存位置。只有当Python的“竞技场”中预先分配的小块的相关部分已满时,才会分配新内存。

这是一个问题吗??你需要对地址做些什么吗??只是一个疑问!!没问题,我只是想知道为什么会这样。是的,我和字典又被放到了另一个地方。地址空间布局随机化在起作用吗?吹毛求疵:
st.\uuuuuu repr\uuuu
不是对象的表示,而是对象的
\uuuu repr\uuu
方法的表示。@Dominicantal无需担心。
st
repr
将是
repr(st)
(或
st.\uuuu repr\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu!