[Python-ideas] try-else without except or finally

Matt Joiner anacrolix at gmail.com
Fri Nov 11 03:48:07 CET 2011


Thanks for clarifying this. If anything, as mentioned earlier, the
meaning of else isn't entirely obvious on the loops, nor is it obvious
on try/except/else/finally clauses. The first time I encountered
for/else I was dumbfounded, then I realised how handy it was. I
wouldn't want to see these constructs removed. They remove the need
for really nasty patterns, I'm sure this was discussed when they were
added.

On Fri, Nov 11, 2011 at 10:26 AM, Terry Reedy <tjreedy at udel.edu> wrote:
> On 11/10/2011 5:21 PM, Matt Joiner wrote:
>>
>> Your examples of else with loops are missing the extra behaviour on
>> break?
>
> I omitted 'break' as not relevant to the particular points I was aiming at.
> To add it in:
>
> while c:
>  x
>  if d: break
> else:
>  y
>
> in PythonGTL could be
>
> label start_loop
> if c:
>  x
>  if d: goto after_loop
>  goto start_loop
> else:
>  y
> label after_loop
>
>> This is where the real value is in this usage.
>
> Yes, 'break' is the main motivation for 'while...else'.
> Return and raise (explicit or otherwise) also break the loop. But since they
> raise stylistic preference issues, I won't go into them here.
>
> --
> Terry Jan Reedy
>
> _______________________________________________
> 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