From introspection perspective, where list values are located in the class object.
If list object is class in python:
>>> a = ['one', 'two']
>>> type(a)
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?
解决方案
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