[Python-ideas] Allowing breaks in generator expressions by overloading the while keyword

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Feb 21 23:33:20 CET 2014


Nick Coghlan wrote:
>     isprime = all(n % p for p in takewhile((: ? ** 2 < n), primes_seen))

-1, this is far too cryptic and ugly to have a place in Python.

I'd be +0 on adding a while clause to for-loops, in
*both* comprehension and statement forms, so that the
above could be written

    isprime = all(n % p for p in primes_seen while p ** 2 < n)

Adding a corresponding clause to the for-statement
would allow the semantic definition of list comprehensions
in terms of the statement expansion to be preserved.

However, this all hinges on whether there is enough
use for the feature to be worth adding. I'm yet to
be convinced of that.

-- 
Greg


More information about the Python-ideas mailing list