python list是否存储对象或对该对象的引用? [英] Does python list store the object or the reference to the object?
问题描述
整数的大小是24个字节,而 char的大小是38个字节,但是当我插入列表时,列表的大小并不反映我插入的对象的确切大小.因此,现在我在游荡列表中保存对象的引用,并且该对象存储在内存中的某个位置.
Size of an integer is 24 bytes and size of a char is 38 bytes, but when i insert into a list the size of the list doesn't reflect the exact size of the object that i insert. So, now I am wandering list is holding the reference of the object and the object is storing somewhere in memory.
>>> sys.getsizeof(1)
24
>>> sys.getsizeof('a')
38
>>> sys.getsizeof([])
72
>>> sys.getsizeof([1])
80
>>> sys.getsizeof(['a'])
80
>>> sys.getsizeof('james')
42
>>>
推荐答案
Python中的所有值都被装箱,它们不会映射到机器类型或大小.也就是说,CPython实现中的所有内容都是一个 PyObject
结构.
All values in Python are boxed, they don't map to machine types or sizes. Namely everything in the implementation of CPython is a PyObject
struct.
http://docs.python.org/2/c-api/structures.html#PyObject
所以,现在我在游荡列表中保存对象的引用,并且该对象存储在内存中的某个位置.
So, now I am wandering list is holding the reference of the object and the object is storing somewhere in memory.
列表也是一个 PyObject,它包含一个序列的引用列表元素的其他PyObject.该列表分配在由Python垃圾收集器管理的Python堆上.
A list is also a PyObject that contains a sequence of references to other PyObjects for the elements of the list. The list is allocated on the Python heap that is managed by the Python garbage collector.
这篇关于python list是否存储对象或对该对象的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!