Reputation: 906
If I have a python list say : ['aaa', 'bbb']
. Is this list stored in 2x8 bytes (for 64-bit addressing) - that is we have only pointers to strings in the list or is it stored in [len('aaa')+len('bbb')]*size_of_char
- that is we have a contiguous storage of characters of each string in the list.
Upvotes: 1
Views: 544
Reputation: 114330
Under the hood in CPython, everything is a pointer to PyObject
. The subtype PyListObject
has a pointer to an array of pointers to PyObjects
among it's structure fields.
Strings are also a subtype of PyObject
, generally implemented in PyUnicodeObject
. Similarly to a list, a string contains a pointer to the buffer containing it's elements.
So the sequence of pointers actually looks like this:
You can deduce the fact that your list buffer can't have [len('aaa') + len('bbb')] * size_of_char
elements from a number of reasons.
In general, if you are curious about the internal workings of CPython, look into the API docs, and the source code.
Upvotes: 2
Reputation: 1518
A way to access python address is to use id().
>>> a=['aaa', 'bbb']
>>> id(a)
62954056
>>> id(a[0])
62748912
>>> id(a[1])
61749544
Further reading is here [understanding-python-variables and memory management].
Upvotes: 3