semicolon at end of python's statements

Antoon Pardon antoon.pardon at rece.vub.ac.be
Fri Aug 30 10:14:39 EDT 2013


Op 30-08-13 12:53, Roy Smith schreef:
> 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.

Maybe python should just allow more than one control structure on one
line and then considers the end of the suite the end of both controls.
In that case we could just write the following:

  for a in lst: if a % 2:
      treat a
      process a some more
  after loop things.

It technically is not loop comprehension but just nested controls but
by allowing them on the same line it looks close enough like loop
comprehension.

Plus if people have a problem that would best be solved by a while loop
comprehension that would be just as easy.

-- 
Antoon Pardon




More information about the Python-list mailing list