gpt4 book ai didi

python - python list 存储的是对象还是对象的引用?

转载 作者:太空狗 更新时间:2023-10-29 17:46:43 25 4
gpt4 key购买 nike

整数的大小是 24 字节字符的大小是 38 字节,但是当我插入列表时,列表的大小并不反射(reflect)我插入的对象的确切大小。所以,现在我在漫游列表中保存着对象的引用,并且对象存储在内存中的某个地方。

>>> 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 结构。

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.

列表也是一个包含 sequence 的 PyObject对列表元素的其他 PyObjects 的引用。该列表分配在由 Python 垃圾收集器管理的 Python 堆上。

关于python - python list 存储的是对象还是对象的引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21178563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com