semicolon at end of python's statements

albert visser albert.visser at gmail.com
Mon Sep 2 13:44:33 EDT 2013


On Mon, 02 Sep 2013 12:58:23 +0200, Antoon Pardon  
<antoon.pardon at rece.vub.ac.be> wrote:

> 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:

[...]

>>>>
>>>> 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 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.
>

My first association would be with the for, but someone could also be  
thinking it's referring to the if on the same line, because there wouldn't  
be any other way to write it (besides nesting the if).

I wouldn't like this syntax anyway, two colons and all.

When I first saw the idea of a nested for .. if construct the thing that  
came to mind was another nesting, namely that of context managers. While I  
don't mind using

     with <context-1>:
         with <context-2>:
             do_stuff

I like being able to do e.g.

     with open('some_file') as _in, open('another_file', 'w') as _out:

because it makes it obvious that the context managers are related.
Expressing that the for and the if are related also appeals to me.
Another parallel might be slicing, where you can specify not only a start  
and an end value, but also an interval (which could be seen as a kind of  
filtering condition).

I think that if you really want to show the "filtered for" as a somewhat  
different concept than a for that just happens to have an if in its suite,  
that should be made visible.
Coming back to my first association mentioned above, why not use a comma?

for <identifier> in <iterable>, <condition>:
     <stuff>

(come to think of it, it has the added bonus that you won't get ambiguity  
what an else might be about).


Somehow this makes sense to me. But then, I also like 'x = y if  
<condition> else z'.
Moreover, I'm Dutch (...)

Albert Visser
-- 
Using Opera's mail client: http://www.opera.com/mail/



More information about the Python-list mailing list