for / while else doesn't make sense

Zachary Ware zachary.ware+pylist at gmail.com
Fri May 20 11:59:59 EDT 2016


On Fri, May 20, 2016 at 3:09 AM, Erik <python at lucidity.plus.com> wrote:
> On 20/05/16 00:51, Gregory Ewing wrote:
>>
>> It's not so bad with "else" because you need to look back
>> to find out what condition the "else" refers to anyway.
>
>
> With my tongue only slightly in my cheek, if it was desirable to
> "fix"/clarify this syntax then I would suggest adding some optional
> (existing) trailing keywords to 'else' in this context that spells it out:
>
> for item in seq:
>     if foo(item):
>         break
> else if not break:
>     nomatch()

With tongue firmly cheeked, you can always use the special `:#` operator:

   for item in seq:
       if foo(item):
           break
   else:# if no break:
       nomatch()

This has the benefit that you can use whatever syntax you like after
the `:#`, and use it in any version of Python you want.

-- 
Zach



More information about the Python-list mailing list