[Python-Dev] PEP 3142: Add a "while" clause to generator expressions
Steven Bethard
steven.bethard at gmail.com
Mon Jan 19 18:28:53 CET 2009
On Mon, Jan 19, 2009 at 7:10 AM, Gerald Britton
<gerald.britton at gmail.com> wrote:
> PEP: 3142
> Title: Add a "while" clause to generator expressions
[snip]
> numbers in that range. Allowing for a "while" clause would allow
> the redundant tests to be short-circuited:
>
> g = (n for n in range(100) while n*n < 50)
>
> would also yield 0, 1, 2, 3, 4, 5, 6 and 7, but would stop at 8
> since the condition (n*n < 50) is no longer true. This would be
> equivalent to the generator function:
>
> def __gen(exp):
> for n in exp:
> if n*n < 50:
> yield n
> else:
> break
> g = __gen(iter(range(100)))
-1. As I pointed out on python-ideas, this proposal makes "while" mean
something different in a generator expression. Currently, you can read
any generator expression as a regular generator by simply indenting
each clause and adding a yield statement. For example:
(n for n in range(100) if n*n < 50)
turns into:
for n in range(100):
if n*n < 50:
yield n
Applying that nice correspondence to the proposed "while" generator
expression doesn't work though. For example:
(n for n in range(100) while n*n < 50)
is, under the proposal, *not* equivalent to:
for n in range(100):
while n*n < 50:
yield n
I'm strongly against making "while" mean something different in a
generator expression than it does in a "while" statement.
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
More information about the Python-Dev
mailing list