semicolon at end of python's statements

Antoon Pardon antoon.pardon at rece.vub.ac.be
Mon Sep 2 06:58:23 EDT 2013


Op 02-09-13 12:42, Fábio Santos schreef:
> On 09/02/2013 10:45 AM, Antoon Pardon wrote:
>> Op 02-09-13 10:05, Steven D'Aprano schreef:
>>> It doesn't keep a whole chain of
>>> if clauses together. It doesn't let you do anything that you haven't
>>> already done. It just saves an indent and a newline. The cost, on the
>>> other hand, includes the risk that people will try to do this:
>>>
>>> for item in seq: if cond:
>>>      do_this()
>>>      do_that()
>>>      else:
>>>          do_something else()
>>>
>>> which is clearly nonsense. Worse is this:
>>>
>>> for item in seq: if cond:
>>>      do_this()
>>>      do_that()
>>> else:
>>>      do_something else()
>>>
>>> which is still nonsense but won't raise SyntaxError.
>> Why shouldn't this raise a SyntaxError?
>>
> Because it would be parsed as a valid for .. else construct. Either that
> or become ambiguous to the programmer, who would not be sure whether he
> was writing an else clause for the `if`, or for the `for`.

I didn't thought about that, but in that case why should we
automatically think of it as nonsense?

I also don't see how this would be that ambigous. The else
lines up with the for, so it seems rather obvious for which
he was writing an else clause.

-- 
Antoon Pardon



More information about the Python-list mailing list