brain stuck. whats occurring here?
gagsl-py2 at yahoo.com.ar
Thu Feb 7 22:13:39 CET 2008
En Thu, 07 Feb 2008 16:16:11 -0200, mensanator at aol.com
<mensanator at aol.com> escribió:
> On Feb 7, 11:38 am, Matthew_WAR... at bnpparibas.com wrote:
> I don't see why you should get either.
> Especially considering this behaviour:
>>>> row=[  for n in range(0,10) ]
> [, , , , , , , , , ]
> [[[...], , , , , , , , , ], , , , , , ,
> , , ]
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 is the same object as row[:]
>>> x = row[:]
>>> x == row
>>> x is row
>>> x is row
In the line a.extend(row[:]), a is THE SAME LIST as row[:], the
first item you are appending. That is, the first thing extend() does is
conceptually a.append(a) - and you got a recursive structure.
> Bug in IDLE?
No, just a misunderstanding of what [:] does, I presume.
More information about the Python-list