aroberge / ideas

Easy creation of custom import hooks to experiment on alternatives to Python's syntax; see https://aroberge.github.io/ideas/docs/html/
Other
79 stars 4 forks source link

Addition to "else" clause in "for" and "while" loops #8

Open aroberge opened 4 years ago

aroberge commented 4 years ago

https://mail.python.org/archives/list/python-ideas@python.org/thread/WNKNETPRGQ3MPQOVG4KG2QV6L7KAPNWM/#N65ZZJPLN6LBPQOURDKJNXWGT64T3ZZK is a discussion on various additions to the else clause in for and while loops.

One of the problems of the suggested syntax is that the use of an if clause can, at first glance, create an ambiguity: is it a stand-alone if clause or part of a loop. Something that could help reduce the ambiguity would be using elif.

Thus, using only existing keywords, it might be interesting to implement something like:

for item in iterable:
    # code block 1
elif pass:
    # code block 2
elif not break:
    # code block 3

which would be translated, using the current syntax, as

looped = False
for item in iterable:
    looped = True
   # code block 1
else:
    if not looped:
        # code block 2
    else:
        # code block 3

The simpler case, corresponding to the existing else clause would be written as

for item in iterable:
    # code block 1
elif not break:
    # code block 2