Op 31-08-13 02:09, Steven D'Aprano schreef:
On Fri, 30 Aug 2013 11:32:17 +0100, Fábio Santos wrote:

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!

There is no need for spam to be "intermediate", and the fact that it
shouldn't be is demonstrated by Ben's error in referring to "process
(spam)" instead of "process(foo)".

We really are spoiled for choice here. We can write any of these:

# Option 1
for spam in sequence:
     if predicate(spam):
         process(spam)

# Option 2
for spam in filter(predicate, sequence):
     process(spam)

# Option 3
for spam in (spam for spam in sequence if predicate(spam)):
     process(spam)


Adding a fourth option:

for spam in sequence if predicate(spam):
     process(spam)

saves absolutely nothing except a line and an indent level, neither of
which are in short supply, and gains nothing in readability over Option 1.

I find this rather disenginuous. Dare to suggest here that python might
benetif by incluidng end markers for its suits because it would make it clearer how many indent levels were done, and chances are someone here
will suggest that too many indent levels are a sign of bad coding
practice, but when someone suggests a change that would allow him
to structure his program more like how he sees it and indent levels
are not short in supply, suggesting there is no problem, no matter how
many one uses.

--
Antoon Pardon
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to