[Python-ideas] Is this PEP-able? for X in ListY while conditionZ:
Eric Snow
ericsnowcurrently at gmail.com
Tue Jun 25 22:59:51 CEST 2013
On Tue, Jun 25, 2013 at 10:55 AM, David Mertz <mertz at gnosis.cx> wrote:
> I'm not quite certain if this is what the OP is proposing, but I think that
> extending comprehensions to allow a 'while' clause would be intuitive and
> somewhat useful. It's true that itertools.takewhile() basically gets us the
> same thing, but actual syntax would be nice, and also more straightforward
> for comprehensions other than generator comprehensions.
While I agree somewhat, the biggest problem is that comprehensions are
already complicated syntax when using the full capabilities. The
simple form is pretty straightforward to learn and read. The if
clause can be confusing at first if not read in context. It can make
the expression longer than you can take in with a glance. Splitting
the comprehension across multiple lines helps with this, so it's
manageable. However, throw "nested" comprehensions into your
expression and you're going to lose just about anyone trying to read
your code.
I think comprehensions are already at the limit of a reasonable level
of complexity. Adding in another supported clause will only make them
harder to learn and harder to read.
-eric
More information about the Python-ideas
mailing list