[Python-ideas] while conditional in list comprehension ?? (original) (raw)

Terry Reedy tjreedy at udel.edu
Thu Jan 31 20:00:35 CET 2013


On 1/31/2013 6:08 AM, Oscar Benjamin wrote:

On 31 January 2013 08:32, Nick Coghlan <ncoghlan at gmail.com> wrote: just so happens that, inside a generator (or generator expression)

raising StopIteration and returning from the generator are very close to being equivalent operations, which is why the "else stop()" trick works. In a 3.x container comprehension, the inner scope is an ordinary function, so the equivalence between returning from the function and raising StopIteration is lost. I don't really understand what you mean here. What is the difference between comprehensions in 2.x and 3.x?

In 2.x, (list) conprehensions are translated to the equivalent nested for and if statements and compiled and executed in place. In 3.x, the translation is wrapped in a temporary function that is called and then discarded. The main effect is to localize the loop names, the 'i' in '[i*2 for i in iterable]', for instance.

-- Terry Jan Reedy



More information about the Python-ideas mailing list