how about:

for something in some_iterable:
    some_stuff_with_maybe_a_break
else if not break:
    something_more

No new keywords :-)

or:

for something in some_iterable:
    some_stuff_with_maybe_a_break
else:  # if not break:
    something_more

and no changes needed to Python!

I may actually start doing that myself ...

As for the "loop didn't run at all" case: Does anyone find a need for that? Personally, I've found that everytime I do some kind of check for an empty iterable before a loop, it was totally unnecessary.

A for loop means: "Do this stuff to all the items in this iterable."

Most of the time, if there's nothing there, you don't need to do the stuff, and that's that.

-CHB






On Tue, Jul 21, 2020 at 5:28 PM Ethan Furman <ethan@stoneleaf.us> wrote:
On 7/20/20 7:34 AM, Barry Scott wrote:

> To avoid the ambiguity of `if` after `for` why not follow `for` with `elif`?
>
> for x in ...:
> ...
>
> elif break:
> # break was called
> elif not break:
> # looped at least once and break not used
> elif pass:
> # same as else today
> # loop'ed no times
>
> (I always have to think what else means after a for).

Keep thinking... ;)

`else` today /does not/ mean "loop'ed no times".  To copy Steven
D'Aprano's example:

>     py> for x in [1,2]:
>     ...     print("inside loop")
>     ... else:
>     ...     print("elif never looped")
>     ...
>     inside loop
>     inside loop
>     elif never looped

> Mistaking the semantics for "if never looped" is a very common mistake.
> Welcome to the club :-)

--
~Ethan~
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/UZFPLNUXQHUAGMCSBSB4RQPSJ7TBH36Z/
Code of Conduct: http://python.org/psf/codeofconduct/


--
Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython