Infinitely nested containers

Ian Kelly ian.g.kelly at gmail.com
Fri Nov 21 13:37:29 EST 2014


On Fri, Nov 21, 2014 at 10:39 AM,  <random832 at fastmail.us> wrote:
> On Fri, Nov 21, 2014, at 02:00, Marko Rauhamaa wrote:
>> Gill Shen <gillarete at gmail.com>:
>>
>> > 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.
>
> 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.

Here's a nice crash. I thought this might similarly produce a
recursion depth error, but no, it's a seg fault!

$ cat test.py
import itertools

l = []
it = itertools.chain.from_iterable(l)
l.append(it)
next(it)
$ python3 test.py
Segmentation fault (core dumped)



More information about the Python-list mailing list