0

In python, a list can contain itself.

a_list = []
a_list.append(a_list)

When printed, it displays [...] for that item. I.E., this:

a_list = [1, 2, 3]
a_list.append(a_list)

would print [1, 2, 3, [...]]. Now, in CPython, how does this work? Does that element of the list contain the ID of the value? Or the memory location? Is their just some sort of special placeholder marker that, when referenced, will replace itself with the original list?

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    @miradulo That technically doesn't describe the implementation, just what it _is_. Now, there's a comment that contains the implementation, but answers are generally easier to search for, so it would be best to have a separate question for exactly that –  Jun 15 '18 at 01:44
  • @miradulo Wait... why did you mark this question as a duplicate of a duplicate? That seems rather inefficient –  Jun 15 '18 at 01:48
  • PascalCase: Python is open-source. Suggest you read the source code for these sorts of questions (because it's the definitive source). – martineau Jun 15 '18 at 01:49
  • @miradulo Yes, that is in fact the case. Hence, the [implementation] tag –  Jun 15 '18 at 01:52
  • 1
    @PascalCase I added a third question that has an answer addressing the internals - it also has over 1000 views. Does that address your question? – miradulo Jun 15 '18 at 01:53
  • I would say the answer is really just a link to the relevant source code. I understand not wanting to hunt down the answer in the code, but if you're not willing to read it, get out. https://hg.python.org/cpython/file/03c65fc349c0/Objects/listobject.c#l361 – J. Doe Jun 15 '18 at 01:56
  • In CPython lists are backed by a primitive array of Py_Object oiinters – juanpa.arrivillaga Jun 15 '18 at 02:08
  • @juanpa.arrivillaga *Py_Object pointers? – J. Doe Jun 15 '18 at 02:24

0 Answers0