[Python-ideas] for/except/else syntax

Bruce Leban bruce at leapyear.org
Wed Oct 7 20:05:04 CEST 2009


On Wed, Oct 7, 2009 at 10:27 AM, MRAB <python at mrabarnett.plus.com> wrote:

> Yuvgoog Greenle wrote:
>
>> I don't like your "except" and "else" only because it's not obvious
>> when they execute.
>>
>> btw your example breaks the current "for..else" search idiom. C,
>> suite_if_loop_is_not_broken, should be labelled
>> suite_if_loop_is_not_broken_and_not_empty in your example code.
>>
>
Some of the discussion makes it clear that it wasn't clear that my
translation was for the for/except/else case and would not change the
for/else case we have today. Here's a translation that covers both cases:

do_except = True
do_else = False
for i in SEQ:
  do_except = False
  A
else:
  do_else = True
if do_except:
  B  # suite_if_loop_body_is_not_executed
elif do_else:
  C

Regarding Guido's comment about the use of 'except' here being it makes
people think of exceptions, I think it makes people think of exceptional
cases too. If I could commandeer the Python time machine and rollback
for/else, what I'd do is:

for i in SEQ:
  suite
except None:
  suite  # executed if SEQ is empty
except NoBreak:
  suite  # executed if loop did not break excluding except None if present

NoBreak is not a keyword; it's a special value.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20091007/3d6c8515/attachment.html>


More information about the Python-ideas mailing list