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-le...@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/