[Python-ideas] if in for-loop statement

Sven R. Kunze srkunze at mail.de
Thu Feb 23 17:07:49 EST 2017


Hey Steven,

On 23.02.2017 19:25, Steven D'Aprano wrote:
> Indeed not. The Pythonic solution is exactly the one you used: DON'T
> combine the for-loop and if-statement.
>
> for x in range(100):
>      if is_prime(x):
>          ...
>
> If the use of two lines and two indents is truly a problem, to the point
> that you really need to refactor to a single line, that's a code smell:
> your function is probably too big, too complex and does too much, and
> should be refactored.

May I disagree with you here? I write many functions with a single for 
loop + a single if+continue on a daily basis (usually generators).
They don't seem to look like code-smell to me.

Not saying they justify this feature but it's easier to read a positive 
(including) if than a negative one (skipping).

> Deeply nested for...for...for...if...if...if...for... blocks look ugly
> because they are ugly. Allowing them all on one line makes the Python
> language worse, not better. It is unfortunate that comprehensions, by
> their nature, must allow that sort of thing, but that doesn't mean we
> have to allow it elsewhere.

Another disagreement here. Just because it's possible to do ugly stuff 
with a hammer shouldn't mean we cannot allow hammers. It's an argument 
neither in favor of nor against the proposal.

Sven


More information about the Python-ideas mailing list