[Python-Dev] Tricky way of of creating a generator via a comprehension expression

Ethan Furman ethan at stoneleaf.us
Wed Nov 22 12:32:09 EST 2017


On 11/22/2017 09:15 AM, Paul Moore wrote:

> 1. List comprehensions expand into nested for/if statements in the
> "obvious" way - with an empty list created to start and append used to
> add items to it.
>     1a. Variables introduced in the comprehension don't "leak" (see below).
> 2. Generator expressions expand into generators with the same "nested
> loop" behaviour, and a yield of the generated value.
> 3. List comprehensions are the same as list(the equivalent generator
> expression).

For what it's worth, that is how I understand it.

--
~Ethan~



More information about the Python-Dev mailing list