# [Python-ideas] Add "while" clauses to generator expressions

Steven Bethard steven.bethard at gmail.com
Sat Jan 10 23:45:59 CET 2009

```[Fixing the top-posting. Note that only Guido is allowed to top-post
around here. ;-)]

On 1/10/09, Steven Bethard <steven.bethard at gmail.com> wrote:
> I think this could end up being confusing. Current generator
> expressions turn into an equivalent generator function by simply
> indenting the clauses and adding a yield, for example:
>
>     (i for i in range(100) if i % 2 == 0)
>
> is equivalent to:
>
>     def gen():
>         for i in range(100):
>             if i % 2 == 0:
>                 yield i
>
> Now you're proposing syntax that would no longer work like this.
>
>     (i for i in range(100) while i <= 50)
>
> I would expect this to mean:
>
>     def gen():
>         for i in range(100):
>             while i <= 50:
>                 yield i

On Sat, Jan 10, 2009 at 2:34 PM, Gerald Britton
<gerald.britton at gmail.com> wrote:
> I guess I don't think it would be confusing. On the contrary, I
> believe that the expressions would read naturally and be a nice
> simplification.  Of course it won't work just like "if" but that is
> just the point!  I can (and do) accomplish the same thing with
> "takewhile", but if the same thing can be sone with a little addition
> to the generator expression, why not do it?

I'm probably just repeating myself here, but the reason not to do it
is that the current generator expressions translate almost directly
into the corresponding generator statements. Using "while" in the way
you've suggested breaks this symmetry, and would make Python harder to
learn.

Steve
--
I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a
tiny blip on the distant coast of sanity.
--- Bucky Katt, Get Fuzzy

```