[Python-ideas] Add "while" clauses to generator expressions
Gerald Britton
gerald.britton at gmail.com
Sat Jan 10 23:34:24 CET 2009
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?
On 1/10/09, Steven Bethard <steven.bethard at gmail.com> wrote:
> On Sat, Jan 10, 2009 at 7:35 AM, Gerald Britton
> <gerald.britton at gmail.com> wrote:
>> I would like to know if anyone has thought of adding a "while" clause
>> as well, like this:
>>
>> a = (i for i in range(100) while i <=50)
>
> 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.
> Taking your example:
>
> (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
>
> In short, -1. You're proposing to use an existing keyword in a new way
> that doesn't match how generator expressions are evaluated.
>
> 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
>
--
Sent from my mobile device
More information about the Python-ideas
mailing list