On 29 Aug 2013 23:20, "Ben Finney" <ben+pyt...@benfinney.id.au> wrote:
>
> Fábio Santos <fabiosantos...@gmail.com> writes:
>
> > It is a shame that this is not possible in python. for..if exists in
> > comprehensions and not in regular loops but that would be nice
> > sometimes.
>
> So you use it in a generator expression, and iterate over the generator:
>
>     for foo in (spam for spam in sequence if predicate(spam)):
>         process(spam)
>
> That way, there's no need for new syntax.

The problem I have with that strategy is that it is repetitive and hinders
readability. You wrote "for" and "in" twice, and spam (a pretty useless
intermediate variable) thrice! While it does its job, it hides the true
intent for filtering beneath a lot of (pun intended) spam. The "if"
particle is nigh undetectable there.

To get around this, I often declare a generator. But I still find it a bit
awkward to have to look up the definition elsewhere, and to waste lines
over something so simple.

I can't say I understand why we don't merge the for loops' syntax with the
comprehension syntax. Even after following the for..while discussion.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to