En Thu, 07 Feb 2008 16:16:11 -0200, [EMAIL PROTECTED]  
<[EMAIL PROTECTED]> escribió:
> On Feb 7, 11:38 am, [EMAIL PROTECTED] wrote:

> I don't see why you should get either.
>
> Especially considering this behaviour:
>
>>>> a=[]
>>>> row=[ [] for n in range(0,10) ]
>>>> a.extend(row[:])
>>>> a
> [[], [], [], [], [], [], [], [], [], []]
>>>> a[0].extend(row[:])
>>>> a
> [[[...], [], [], [], [], [], [], [], [], []], [], [], [], [], [], [],
> [], [], []]

Those [...] should give a clue. Usually Python doesn't "shorten" a list  
representation: if it takes a thousand lines to output a list, there will  
be a thousand lines of output. The [...] means that the list is  
*recursive*: it has an element that refers to the list itself, so it can't  
be represented in the normal way.
Why is it recursive? row[:] is a new list, not the same object as row. It  
is a copy - but a "shallow" copy, because their elements aren't copies  
themselves. So row[0] is the same object as row[:][0]

>>> x = row[:]
>>> x == row
True
>>> x is row
False
>>> x[0] is row[0]
True

In the line a[0].extend(row[:]), a[0] is THE SAME LIST as row[:][0], the  
first item you are appending. That is, the first thing extend() does is  
conceptually a[0].append(a[0]) - and you got a recursive structure.

> Bug in IDLE?

No, just a misunderstanding of what [:] does, I presume.

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to