[Python-ideas] for/else syntax

Yuvgoog Greenle ubershmekel at gmail.com
Fri Oct 2 05:11:22 CEST 2009


Let me rephrase - for..else is a _misleading_ construct. It suggests one
thing yet does another.
This response is way too long, so here's a TOC:
1. The Question Challenge - A way to examine if "for..else" is just
unintuitive or misleading.
2. What can be done about for...else.
3. Lets talk about "try/else" because a few people mentioned it as though
it's somehow related to the discussion.

----------------
1. The Question Challenge - A way to examine if "for..else" is just
unintuitive or misleading.

Antti's experiment got me asking, you can try this as well. Ask a person who
knows python the following questions:
Question #1: Do you know what happens when an "else" appears after a "for"
loop?
    if they answer yes then say nevermind.
    otherwise: continue to question #2.
Question #2: When would the code in the "else" block execute? If you don't
know, it's ok to guess.
    If they answer "when a break didn't occur", I'm willing to give you, the
asker, up to 100 lines of patch code in whatever python project you want..
    If they answer "maybe when the for loop didn't run or was an empty list"
then you don't owe me anything.

-------------
2. What can be done about for...else.

1. Not allowing a for loop that has no "break" to have an "else". Just like
"else" isn't allowed when there isn't an "except" in the "try". There really
is no excuse, justification or sense in a "for" loop that has no "break" yet
has an "else".
2. Lets not talk about removing the "else" from for/while completely since
it does have its fans and cute use cases.
3. Renaming this construct for py4k wouldn't be too crazy if you use the
existing reserved words "not" and "break". Look at this code and please note
that it doesn't need any comments to make it readable (as opposed to Nick's
example, no offence):

for item in list_of_stuff:
    if item.is_awesome():
        break
not break:
    print("nothing is awesome :(")

And I can even think of a few use cases for having just "break:". This block
would execute only upon "break" occurring. That way you could have loops
like this:

for item in list_of_stuff:
    if item.is_awesome():
        break
break:
    celebrate(list_of_stuff)

--------------------------
3. Lets talk about "try/else" because a few people mentioned it as though
it's somehow related to the discussion.

"try/else" now _that_ is a red herring. There is no "try/else". OTOH
"except/else", as I said before, is a beautiful, pythonic, useful and cool
construct.

This is the evidence:

>>> try:
...     print(1)
... else:
  File "<stdin>", line 3
    else:
       ^
SyntaxError: invalid syntax
>>> try:
...     print(2)
... finally:
...     print(3)
... else:
  File "<stdin>", line 5
    else:
       ^
>>> try:
...     print(7)
... except Exception as e:
...     print(13)
... else:
...     print(6)
...
7
6
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20091002/54b99485/attachment.html>


More information about the Python-ideas mailing list