Infinitely nested containers

Steven D'Aprano steve+comp.lang.python at
Sat Nov 22 05:43:19 CET 2014

random832 at wrote:

> On Fri, Nov 21, 2014, at 02:00, Marko Rauhamaa wrote:
>> Gill Shen <gillarete at>:
>> > How is this [nesting] behavior implemented under the hood?
>> Pointers.
>> > And why is this allowed at all?
>> There's no reason not to.
> There's no reason not to allow it with tuples, but you can't do it.
> Mainly because doing it in a single literal would require special
> syntax, whereas you can simply append to a list a reference to itself.

You can't append a list to itself in a single expression, you have to create
the list first.

> I think I tried on at least one python version and printing the tuple
> crashed with a recursion depth error, since it had no special protection
> for this case the way list printing does.

It works fine now (Python 3.3).

py> L = []
py> t = (L, None)
py> L.append(L)
py> L.append(t)  # For good measure.
py> print(t)
([[...], (...)], None)

but yes, in old versions of Python printing self-recursive containers may


More information about the Python-list mailing list