semicolon at end of python's statements
Roy Smith
roy at panix.com
Fri Aug 30 06:53:27 EDT 2013
In article <mailman.385.1377858745.19984.python-list at python.org>,
Fábio Santos <fabiosantosart at gmail.com> wrote:
> On 29 Aug 2013 23:20, "Ben Finney" <ben+python at benfinney.id.au> wrote:
> >
> > Fábio Santos <fabiosantosart at 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.
+1 on loop comprehensions, for all the reasons Fábios states.
More information about the Python-list
mailing list