Cryptic list object resentation in Python as [...]

Viewed 55

I've run into this unseen list object, when I tried to play with list append, and I've searched hard but unable to find much information. So this is what's happening:

L = ['dinosaur']
L.append(('theropoda', L))
print(L)
# ['dinosaur', ('theropoda',   [...])]

Questions - what's the [...] means here? Thanks.

1 Answers

As mentioned in the comments, Python will not attempt to include a circular/recursive reference in the representation of a list.

It would appear that the __repr__ function (also used by lists to create the string for printing) is implemented via reprlib with recursive support. Without it, you would end up with a RecursionError as to output the list, Python must include the nested version of the list, which also needs the nested version, and so on. Instead, it outputs the special value of ... which indicates to you that it is a recursive reference.

Related