Re: Infinitely nested containers

2014-11-22 Thread random832
On Sun, Nov 23, 2014, at 00:59, Steven D'Aprano wrote: > >> 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) > > > > This is a tuple in a list in a tuple, not

Re: Infinitely nested containers

2014-11-22 Thread Steven D'Aprano
Ethan Furman wrote: > On 11/21/2014 08:43 PM, Steven D'Aprano wrote: >> random...@fastmail.us wrote: >>> >>> 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 d

Re: Infinitely nested containers

2014-11-22 Thread Terry Reedy
On 11/21/2014 2:30 PM, Zachary Ware wrote: On Fri, Nov 21, 2014 at 12:37 PM, Ian Kelly wrote: 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)

Re: Infinitely nested containers

2014-11-22 Thread Ethan Furman
On 11/21/2014 08:43 PM, Steven D'Aprano wrote: > random...@fastmail.us wrote: >> >> 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

Re: Infinitely nested containers

2014-11-21 Thread Rustom Mody
On Saturday, November 22, 2014 10:40:23 AM UTC+5:30, Chris Angelico wrote: > On Sat, Nov 22, 2014 at 4:02 PM, Rustom Mody wrote: > > Thats not a single expression; which is possible with a lazy > > evaluation language like Haskell. > > Prelude> let ones = 1 : ones > > I'm not sure "lazy evaluatio

Re: Infinitely nested containers

2014-11-21 Thread Chris Angelico
On Sat, Nov 22, 2014 at 4:02 PM, Rustom Mody wrote: > Thats not a single expression; which is possible with a lazy > evaluation language like Haskell. > Prelude> let ones = 1 : ones I'm not sure "lazy evaluation" is the key here, unless it also does name lookups lazily. What happens if you do thi

Re: Infinitely nested containers

2014-11-21 Thread Rustom Mody
On Saturday, November 22, 2014 10:20:36 AM UTC+5:30, Chris Angelico wrote: > On Sat, Nov 22, 2014 at 3:43 PM, Steven D'Aprano wrote: > > random832 wrote: > > > >> 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 speci

Re: Infinitely nested containers

2014-11-21 Thread Chris Angelico
On Sat, Nov 22, 2014 at 3:43 PM, Steven D'Aprano wrote: > random...@fastmail.us wrote: > >> 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 its

Re: Infinitely nested containers

2014-11-21 Thread Steven D'Aprano
random...@fastmail.us wrote: > On Fri, Nov 21, 2014, at 02:00, Marko Rauhamaa wrote: >> Gill Shen : >> >> > 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

Re: Infinitely nested containers

2014-11-21 Thread Zachary Ware
On Fri, Nov 21, 2014 at 12:37 PM, Ian Kelly wrote: > 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.p

Re: Infinitely nested containers

2014-11-21 Thread Ian Kelly
On Fri, Nov 21, 2014 at 10:39 AM, wrote: > On Fri, Nov 21, 2014, at 02:00, Marko Rauhamaa wrote: >> Gill Shen : >> >> > 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 a

Re: Infinitely nested containers

2014-11-21 Thread random832
On Fri, Nov 21, 2014, at 12:47, Chris Angelico wrote: > You can do it in C, I believe - PyTuple_New() followed by > PyTuple_SetItem(x, 0, x) should do it. Yeah, that's how I did it. I think python 2 crashed and python 3 didn't... or maybe it was the interactive interpreter that crashed and calling

Re: Infinitely nested containers

2014-11-21 Thread Chris Angelico
On Sat, Nov 22, 2014 at 4:39 AM, wrote: > 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 ver

Re: Infinitely nested containers

2014-11-21 Thread random832
On Fri, Nov 21, 2014, at 02:00, Marko Rauhamaa wrote: > Gill Shen : > > > 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 beca

Re: Infinitely nested containers

2014-11-21 Thread Ian Kelly
On Thu, Nov 20, 2014 at 10:54 PM, Gill Shen wrote: > How is this behavior implemented under the hood? And why is this allowed at > all? Is it just a curiosity or can you do something useful with it? Reference cycles are common in Python and other OO languages. For example, adding a widget to a w

Re: Infinitely nested containers

2014-11-20 Thread Marko Rauhamaa
Gill Shen : > How is this [nesting] behavior implemented under the hood? Pointers. > And why is this allowed at all? There's no reason not to. > Is it just a curiosity or can you do something useful with it? It absolutely does arise every here and there. It's so natural you don't even pay att

Re: Infinitely nested containers

2014-11-20 Thread Chris Angelico
On Fri, Nov 21, 2014 at 4:54 PM, Gill Shen wrote: > How is this behavior implemented under the hood? And why is this allowed at > all? Is it just a curiosity or can you do something useful with it? Simple: The list contains itself. When you dereference the list, you get back the original list. I