[Python-ideas] Is this PEP-able? for X in ListY while conditionZ:

Steven D'Aprano steve at pearwood.info
Mon Jul 1 22:29:51 CEST 2013


On 02/07/13 03:25, Joshua Landau wrote:

> If you're telling me that "{p for p in primes() while p < 100}" reads
> better than "{p for p in primes(); break if p >= 100}" I have to
> disagree strongly. The "break if" form looks beautiful.

Beautiful it may be, but you have something which explicitly looks like *two statements* which actually represents *one expression*.

When it comes to mimicry and camouflage, I think that it is wonderful and astonishing when insects look like sticks, plants look like insects, and harmless snakes look like deadly ones. But when it comes to Python code, I think that mimicry is a terrible idea. I am truly not looking forward to fielding questions from confused programmers who extrapolate from the above to multiple statements in a comprehension:

{p for p in primes(); print(p); break if p >= 100}

and other variations:

{x for x in values(); raise ValueError if not condition(x)}

And I'm even *less* looking forward to the debates on python-ideas from people who will want to introduce such syntax :-)

"Clever" syntax is rarely a good idea. Good syntax should allow the reader to correctly extrapolate to other examples, not mislead them into making errors.


-- 
Steven


More information about the Python-ideas mailing list