Re: cyclic data structures

2006-02-14 Thread Ewald R. de Wit
John Salerno wrote: I'm having some slight trouble understanding exactly why this creates an infinite loop: L = [1, 2] L.append(L) I tried this with Python 2.3.5 and it handles this tailbiter in a very pleasantly surprising way: l = [ 0, 1 ] l.append( l ) l [0, 1, [...]] l[2] [0, 1,

cyclic data structures

2006-02-13 Thread John Salerno
I'm having some slight trouble understanding exactly why this creates an infinite loop: L = [1, 2] L.append(L) In a highly abstract way, I understand it. But if I were asked to write down what I think L is after the second line executes, I would write: [1, 2, [1, 2]] But the correct answer

Re: cyclic data structures

2006-02-13 Thread Peter Decker
On 2/13/06, John Salerno [EMAIL PROTECTED] wrote: I'm having some slight trouble understanding exactly why this creates an infinite loop: L = [1, 2] L.append(L) 'L' is a pointer to a list. You are now adding that pointer to the very list it points to. What you're looking for is: L = [1, 2]

Re: cyclic data structures

2006-02-13 Thread John Salerno
Peter Decker wrote: 'L' is a pointer to a list. You are now adding that pointer to the very list it points to. I understand that, but I guess I just don't see how this creates anything other than a list that refers to [1, 2], and then refers again to [1, 2], to create [1, 2, [1, 2]].

Re: cyclic data structures

2006-02-13 Thread Fredrik Lundh
John Salerno wrote: L.append(L) basically creates this, I think: [1, 2, L] Now, assuming that's correct, and since L points to the list [1, 2] L points to [1, 2, L] /F -- http://mail.python.org/mailman/listinfo/python-list

Re: cyclic data structures

2006-02-13 Thread Carsten Haese
On Mon, 2006-02-13 at 16:03, John Salerno wrote: Peter Decker wrote: 'L' is a pointer to a list. You are now adding that pointer to the very list it points to. I understand that, but I guess I just don't see how this creates anything other than a list that refers to [1, 2], and then

Re: cyclic data structures

2006-02-13 Thread Duncan Booth
John Salerno wrote: L.append(L) basically creates this, I think: [1, 2, L] Correct. Now, assuming that's correct, and since L points to the list [1, 2], wrong. You just said it yourself: L is now the list [1, 2, L] why can't '[1, 2]' be substituted for the 'L' and then the list is

Re: cyclic data structures

2006-02-13 Thread John Salerno
Carsten Haese wrote: L is [1,2] before the append. After the append, L is [1,2,L], which is [1,2,[1,2,L]], which is [1,2,[1,2,[1,2,L]]] etc into infinity. L literally contains itself after the append. Ah, this perfectly explains it now! I guess I was dancing around this fact, but now that