[Python-Dev] PEP 3142: Add a "while" clause to generator expressions

Daniel Stutzbach daniel at stutzbachenterprises.com
Mon Jan 19 17:15:38 CET 2009


On Mon, Jan 19, 2009 at 9:10 AM, Gerald Britton <gerald.britton at gmail.com>wrote:

>      g = (n for n in range(100) if n*n < 50)
>
>  would yield 0, 1, 2, 3, 4, 5, 6 and 7, but would also consider
>  the numbers from 8 to 99 and reject them all since n*n >= 50 for
>  numbers in that range.  Allowing for a "while" clause would allow
>  the redundant tests to be short-circuited:
>

Instead of using a "while" clause, the above example could simply be
rewritten:

    g = (n for n in range(8))

I appreciate that this is a toy example to illustrate the syntax.  Do you
have some slightly more complex examples, that could not be rewritten by
altering the "in" clause?

--
Daniel Stutzbach, Ph.D.
President, Stutzbach Enterprises, LLC <http://stutzbachenterprises.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20090119/57578def/attachment-0001.htm>


More information about the Python-Dev mailing list