Python中List的底层结构

2024-03-29 14:23:28 发布

您现在位置:Python中文网/ 问答频道 /正文

根据我读过的资料,python中的列表数据结构是用数组实现的。但是,python列表可以包含不同类型的对象。如果它的底层数据结构是数组,那么数组如何包含不同类型的对象?在

我错了数组是特定的数据类型吗?在


Tags: 对象数据结构类型列表数组数据类型底层资料
3条回答

我给你一个程序例子,你可以很容易地理解对象是如何存储在列表中的

class my_class:
    def __init__(self):
        self.var=1
    def setparameter(self,txt):
        self.txt=txt

objects = [my_class() for i in range(3)] # creates multiple objects here
for obj in objects:
    obj.setparameter(5)

列表中的对象是类实例的指针。查看其数据结构

enter image description here

请参阅另一个示例这些列表是子列表的指针

列表列表

^{pr2}$

enter image description here

你错了,它是一个特定类型的数组。Python列表被实现为对其他Python对象(直接或间接地,内存地址)的引用数组。因为还有很多家务活要做。在

Python列出了对数据的存储引用,而不是数据本身。直接来自python源代码^{}

Objects are always accessed through pointers of the type 'PyObject *'. The type 'PyObject' is a structure that only contains the reference count and the type pointer. The actual memory allocated for an object contains other data that can only be accessed after casting the pointer to a pointer to a longer structure type.

如果您需要更详细的信息,源代码中的注释是详细的和描述性的。在

相关问题 更多 >