views:

108

answers:

3

Take the following example:

>>> for item in [i * 2 for i in range(1, 10)]:
    print item


2
4
6
8
10
12
14
16
18

Is [i * 2 for i in range(1, 10)] computed every time through the loop, or just once and stored? (Also, what is the proper name for that part of the expression?)

One reason I would want to do this is that I only want the results of that list comprehension to be available in the loop.

+3  A: 

In that case, you are constructing a list in memory and then you are iterating over the contents of the list, so yes, it is computed once and stored. it is the no different than doing

i for i in [2,4,6,8]:
   print(i)

If you do iter(i * 2 for i in xrange(1,10)), you get an iterator which evaluates on each iteration.

mikerobi
+3  A: 

All members of the expression list are calculated once, and then iterated over.

In Python 2.x, the variable used in a LC does leak out into the parent scope, but since the LC has already been evaluated, the only value available is the one used to generate the final element in the resultant list.

Ignacio Vazquez-Abrams
+5  A: 

A good translation of for i in <whatever>: <loopbody>, showing exactly what it does for any <whatever> and any <loopbody>:

_aux = iter(<whatever>)
while True:
  try: i = next(_aux)
  except StopIteration: break
  <loopbody>

except that the pseudo-variable I have here named _aux actually remains unnamed.

So, <whatever> always gets evaluated just once (to get an iter() from it) and the resulting iterator is nexted until it runs out (unless there's some break in the <loopbody>).

With a listcomp, as you've used, the evaluation produces a list object (which in your code sample remains unnamed). In the very similar code:

for item in (i * 2 for i in range(1, 10)): ...

using a genexp rather than the listcomp (syntactically, round parentheses instead of the listcomp's square brackets), it's the next() that actually does most of the work (advancing i and doubling it), instead of lumping all work at construction time -- this takes up less temporary memory, and may save time if the loop body's reasonably likely to break out early, but except in such special conditions (very tight memory or likely early loop termination) a listcomp may typically be (by a wee little bit) faster.

Alex Martelli
In that case, in GAE, `for item in ModelClass.all():` is ok, right?
Rosarch
@Rosarch, yep, that's right.
Alex Martelli