[Python-ideas] try-else without except or finally
Matt Joiner
anacrolix at gmail.com
Mon Nov 14 22:21:23 CET 2011
> While it is redundant to allow else without except, I would agree that
yeah that nails it.
> it should be allowed for the same reason that else is allowed on for> and while without a break. The alternative would be to make the later
that's a really good point, i hadn't considered this. similar things
also occur when you "drop" out of some construct because you can
predict the control flow. for example when certain blocks are
guaranteed to throw exceptions, it becomes unnecessary to put
alternative behaviour in an "else" block.
try:
a
except:
b
raise
c
In the above, c does not get put in an else because it's redundant.
The same applies here:
if a:
raise b
c
> illegal. Either seems reasonable, however the former maintains> backwards compatibility, while the later breaks it.
Yes for clarification you're saying:
while a:
b
else:
c
Is redundant if b does not break.
I think this puts the idea of else without except into the "why not?"
camp for me.
On Tue, Nov 15, 2011 at 7:04 AM, Chris Kaynor <ckaynor at zindagigames.com> wrote:
> On Mon, Nov 14, 2011 at 11:34 AM, Matt Joiner <anacrolix at gmail.com> wrote:
>> To be fair it's not a new construct, it's loosening the constraints on
>> an existing one.
>>
>> else has the same use as in the for and while compound statements:
>> It's executed if the first block completes "successfully". An
>> exception being thrown in a try block, cancels execution of the "else"
>> block, the same way as "break" in the for and while statements.
>>
>> I have a problem with the idea now in that:
>>
>> try:
>> a
>> else:
>> b
>>
>> is the same as:
>>
>> try:
>> a
>> b
>> else:
>> pass
>>
>> and even:
>>
>> try:
>> pass
>> else:
>> a
>> b
>>
>> in every possible interpretation. This isn't possible with if, for,
>> while, try/except, and try/finally compound statements, all of which
>> alter the meaning in *some* way.
>
> If you add finally, the else clause still maintains the same semantics
> in a try statement - only except statements modify the equivalence of
> else:
> try:
> a
> else:
> b
> finally:
> c
>
> is the same as:
> try:
> a
> b
> else:
> pass
> finally:
> c
>
> and
>
> try:
> pass
> else:
> a
> b
> finally:
> c
>
> What changes with the finally is that you cannot move the "b"
> statement out of the try completely. Without the finally clause, the
> following are the same:
>
> try:
> a
> else:
> b
>
> try:
> a
> b
>
> a
> b
>
>
>
>
> Similar behavior can be shown with for: else and while: else, if there
> is no break (I'm only showing a while statement below, however it
> converts exactly to a for statement):
> while a:
> b
> else:
> c
>
> is the same as:
> while a:
> b
> c
>
> so long as "b" does not include a break statement.
>
>
>
>>
>> So I maintain that the else block could be made allowable even without
>> an except block, but it would be purely for convenience. I don't think
>> it's worth it anymore.
>
> While it is redundant to allow else without except, I would agree that
> it should be allowed for the same reason that else is allowed on for
> and while without a break. The alternative would be to make the later
> illegal. Either seems reasonable, however the former maintains
> backwards compatibility, while the later breaks it.
>
>>
>> Cheers all for consideration.
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
More information about the Python-ideas
mailing list