Where does a Python list hold its values?

左心房为你撑大大i 提交于 2019-12-10 19:51:56

问题


From introspection perspective, where list values are located in the class object. If list object is class in python:

>>> a = ['one', 'two']
>>> type(a)
<class 'list'>

So it is stored somewhere in the class, but where?

For example: If we define a class with values:

class Test:
    def __init__(self):
        self.test_name = "Do Not"
        self.test_surname = "Know"

It is easy to locate an instance values:

>>> b = Test()
>>> print(a.__dict__)
{'test_surname': 'Know', 'test_name': 'Do not'}

Is there similar option to reach those values in the list class object?


回答1:


This is really up to the implementation detail. In cpython, the container object only hold references (pointers) to the stored values. Any operation involving the list internals only manipulates the pointers, not the objects.

Memory is over-allocated so that there are always some free slots available, which makes appends and inserts faster. The space allocated increased by about 12.5% when full. You can actually see that yourself by appending to a list and calling sys.getsizeof in a loop:

>>> import sys
>>> l = []
>>> for i in range(100):
...     print(sys.getsizeof(l)),
...     l.append(None)
...     
72 104 104 104 104 136 136 136 136 200 200 200 200 200 200 200 200 272 272 272 272 272 272 272 272 272 352 352 352 352 352 352 352 352 352 352 440 440 440 440 440 440 440 440 440 440 440 536 536 536 536 536 536 536 536 536 536 536 536 648 648 648 648 648 648 648 648 648 648 648 648 648 648 776 776 776 776 776 776 776 776 776 776 776 776 776 776 776 776 920 920 920 920 920 920 920 920 920 920 920

You can not find a dict of the items behind the scenes somewhere, like you have done with the attributes. The list itself is merely an array storing it's length and the memory locations of the items.

image source: here




回答2:


Not really. The list items aren't stored under any accessible property of the list object. It's all just low-level implementation details of the language.

However, if you want to subclass list and add or remove an item, you can just run list operations on self.

In [67]: class TestList(list):
   ....:     def __init__(self, defaultElement):
   ....:         self.append(defaultElement)
   ....:

In [68]: TestList('sample')
Out[68]: ['sample']

...

In [72]: TestList('sample') + ['3']
Out[72]: ['sample', '3']

Also note that dict() itself doesn't have a __dict__, either:

In [73]: dict().__dict__
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-73-58263694ff9c> in <module>()
----> 1 dict().__dict__

AttributeError: 'dict' object has no attribute '__dict__'

You can find the source implementation for list() here in CPython's Objects/listobject.c.




回答3:


Is there similar option to reach those values in the list class object?

Yes. It's the list itself. For example, if you define a:

a = ['one', 'two']

Then the list values are in a.



来源:https://stackoverflow.com/questions/37494021/where-does-a-python-list-hold-its-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!