On 11/07/2020 06:22, Олег Комлев wrote:
ELSE-clause in FOR and WHILE has unclear syntax. I suggest new clause instead:
if COND:
...
[elif COND:
...]
[else:
...]
This IF-clause like must be immediately after FOR- or WHILE-cycle (only comment
allowed between). It looks like a regular IF, but COND is special.
COND may be "break", "pass" or "finally". "if break:" - if used break-operator to exit cycle. "if pass:" - cycle executed 0
times. "if finally:" - cycle executed 0 or more times ("pass-case" is included in "finally-case"). For compatibility only "else:" means
"if finally:".
It's compatible enhancement. No new keyword. There can be no combination "break", "pass" or
"finally" after "if"/"elif:" in current version.
-----------------
This idea was suggested as enhancement issue
(https://bugs.python.org/issue41272).
Eric V. Smith (eric.smith) advised me to put this on the python-ideas mailing
list (https://bugs.python.org/issue41272#msg373486).
_______________________________________________
Just to spell it out: If I understand you correctly, you are proposing
that after a 'for'/'while' loop
'if break:' has its intuitive meaning: "if the loop was terminated
with a 'break'".
'if finally:' means "if the loop completed (possibly with zero
iterations) *without* a 'break'"
(equivalent to 'else' at present).
'if pass:' means "if there were zero iterations of the loop
(obviously, without a 'break')"
(I assume that a partial iteration terminated by 'continue' would
count as an iteration)
plus 'elif' when you want to handle more than one of the above. So
if COND1:
...
elif COND2:
...
would be legal, but
if COND1:
...
if COND2:
...
would not.
Something to consider: Would it be legal to mix these CONDs with other
conditions in the 'elif' chain? E.g.
if break:
...
elif x > 0:
...
elif finally:
...
I would be the first to agree that the use of 'else' after 'for'/'while'
is one of Python's more obscure features (I understand even veterans
trip up over it).
I like the general idea and I like 'if break:', but IMO the meanings of
the other two are not obvious.
I would suggest 'if not break:' rather than 'if finally:'. (And PEP 8
could recommend it rather than 'else:'. :-))
My gut feeling (backed by no evidence) is that dealing with the case of
zero iterations is not needed frequently enough to cater for it.
And I don't know if there is a good way to spell it (which might be
another reason not to do it).
Plus it's something which could be added later.
Best wishes
Rob Cliffe
_______________________________________________
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/BDGPJVV4L7PFG23TNFUR22U22SIJ6PJR/
Code of Conduct: http://python.org/psf/codeofconduct/