Say you do the following:
a = [1]
a[0] = a
You end up with a
as equal to [[...]]
. What\'s going on here? How doe
The list contains a reference to itself. The [[...]]
is how this is rendered when you print the list.
The implementation goes out of its way to ensure it doesn't end up in an infinite recursion. It does this by rendering references to objects that are already being printed as [...]
.
This makes it work with indirect self-references too:
>>> a = []
>>> b = [a]
>>> a.append(b)
>>> a
[[[...]]]
>>> b
[[[...]]]
If you are really curious, you could study CPython's source code. In Python 2.7.3, the relevant code is located in Objects/listobject.c
.