Bug in list comprehensions?

Sion Arrowsmith siona at chiark.greenend.org.uk
Wed Jun 7 07:43:08 EDT 2006


Fredrik Lundh  <fredrik at pythonware.com> wrote:
>Iain King wrote:
>> I'm guessing I'm the one confused here... but I'm confused!  What's 
>> going on?
>the clauses nest from left to right, not from right to left, so "[x for 
>x in y for y in beta]" is equivalent to
>
>      out = []
>      for x in y:
>          for y in beta:
>              out.append(x)

And a list comprehension doesn't get a namespace to itself (cf.
generator comprehensions) so "leaks" its variables. Exactly as
above. So the y being iterated over in "for x in y" is the y
from the previous inner iteration ("for y in beta").

-- 
\S -- siona at chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |    -- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump



More information about the Python-list mailing list