On Mon, Nov 30, 2009 at 9:22 AM, Manuel Graune <span dir="ltr"><<a href="mailto:manuel.graune@koeln.de">manuel.graune@koeln.de</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
in (most) python documentation the syntax "list()"<br>
and "[]" is treated as being more or less the same<br>
thing. For example "help([])" and "help(list())" point<br>
to the same documentation. Since there are at least<br>
two cases where this similarity is not the case, (see below)<br>
can someone explain the reasoning behind this and point to<br>
further / relevant documentation?<br>
(To clarify: I am not complaining about this, just asking.)<br></blockquote><div><br></div><div>They -do- pretty much the same thing, but you can't quite treat them as two forms which can be exchanged at a whim.</div>
<div><br></div><div>[] is the syntax for creating a list literal in source code. Its also the syntax for list comprehensions. A list comprehension is an expression that constructs a list all at once.</div><div><br></div>
<div>
list() is a function (type/constructor/...) which accepts any iterable as an argument (or None), and uses it to construct a list, returning it.</div><div><br></div><div>Those two things are very different, although in the end they both produce lists: one is syntax, the other is a function which has to be called.</div>
<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">1.)<br>
<br>
when using local variables in list comprehensions, say<br>
<br>
a=[i for i in xrange(10)]<br>
<br>
the local variable is not destroyed afterwards:<br>
<br>
print "a",a<br>
print "i",i<br>
<br>
using the similar code<br></blockquote><div><br></div><div>List comprehensions "leaked", as more of an implementation detail I believe then anything else: in Python 3 it was removed, to better match generator expressions (IIRC).</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
b=list(j for j in xrange(10))<br>
<br>
the local variable is destroyed after use:<br>
<br>
print "b",b<br>
print "j",j<br>
<br></blockquote><div><br></div><div>But see, this is something completely different. </div><div><br></div><div> (j for j in xrange(10))</div><div><br></div><div>is NOT a list comprehension. Its a generator comprehension. In the first example with lists, you're using literal syntax to construct (all at once) a list. In this, you're using literal syntax to construct a generator-- the generator is then passed to the list function, which iterates over it and constructs a list, returning when that generator is exhausted.</div>
<div><br></div><div>And generator comprehensions don't leak their variables (I can't remember if that was an implementation detail or a deliberate design decision, but eventually list comprehensions were made to match the behavior).</div>
<div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
and 2)<br>
<br>
a=list([])<br>
<br>
vs.<br>
<br>
b=[[]]<br></blockquote><div><br></div><div>Those are also doing two completely different things: on the first, you are using literal syntax to create an empty list. You are then passing this list (which is an iterable) to the list function, telling it to make a list out of that literal. It does so, which is in the end a (shallow) copy of the first list you made.</div>
<div><br></div><div>The second, you are using literal syntax to create an empty list, and using literal syntax to place that empty list inside another list.</div><div><br></div><div>You basically can't treat the two things as interchangable, because they're different: one is syntax, one is a function. Both are ways to make lists.</div>
<div><br></div><div>HTH,</div><div><br></div><div>--S</div></div>