[Python-ideas] for/else syntax
Yuvgoog Greenle
ubershmekel at gmail.com
Fri Oct 2 18:00:08 CEST 2009
On Fri, Oct 2, 2009 at 6:34 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> Steven D'Aprano wrote:
>> On Fri, 2 Oct 2009 09:06:11 pm Nick Coghlan wrote:
>>
>>> Just as there is no "try/else",
>>
>> Perhaps there should be. Does anyone know why there isn't? It seems an
>> arbitrary restriction to me, because the following pieces of code are
>> not equivalent:
>>
>> # not currently legal
>> try:
>> if cond:
>> raise ValueError
>> else:
>> print "no exception was raised"
>> finally:
>> print "done"
>
> Just drop the else line and you get the semantics you're after:
>
> try:
> if cond:
> raise ValueError
> print "no exception was raised"
> finally:
> print "done"
>
Also, in English/programmer/pesudo-code/human language, the following:
something:
A
else:
B
Conveys that either A occurred or B occurred. This is just something
everybody is used to because everybody knows if/else. Consider the
following code:
except:
print("A brave attempt")
else:
print("Yippee")
It's easy to understand that either "except" occurred or "else"
occurred. The only time this rule is broken in python is the for/else
and while/else.
More information about the Python-ideas
mailing list