This is what your code created
It's a list where the first and last elements are pointing to two numbers (1 and 2) and where the middle element is pointing to the list itself.
In Common Lisp when printing circular structures is enabled such an object would be printed as
#1=#(1 #1# 2)
meaning that there is an object (labelled 1 with #1=
) that is a vector with three elements, the second being the object itself (back-referenced with #1#
).
In Python instead you just get the information that the structure is circular with [...]
.
In this specific case the description is not ambiguous (it's backward pointing to a list but there is only one list so it must be that one). In other cases may be however ambiguous... for example in
[1, [2, [...], 3]]
the backward reference could either point to the outer or to the inner list.
These two different structures printed in the same way can be created with
x = [1, [2, 3]]
x[1][1:1] = [x[1]]
y = [1, [2, 3]]
y[1][1:1] = [y]
print(x)
print(y)
and they would be in memory as
p = [1]; p[0] = p
. – Rennesp 3: [1, <Recursion on list with id=3074777548>, 2]
. What did you run? – Schoonmakerid=3074777548
is the id ofp
so its easy to understand that its referring to itself. Anyway very nice question @Zel – Juanitajuanne