Hacker News new | past | comments | ask | show | jobs | submit login

What finally made it intuitive for me: assuming the loop contains an if condition: break, you can consider the else-clause to be the else of the if statement within the loop.



I think the real issue is that the word 'else' is ambiguous in the context. Everyone comes in with a different idea of what the 'else' is a fallback clause to. Guido obviously picked one particular case, but there are other valid meanings.

For example, I think that it's more common to want to do something like:

  # mnemonic:
  #   for thing in list_of_things DO_SOMETHING else DO_SOMETHING_ELSE

  if list_of_things:
    for thing in list_of_things:
      print thing
  else:
    print "No things!"
Than:

  for thing in list_of_things:
    if thing.is_awesome:
      break
  else:
    print "No awesome things!"
And what about conditional code that you want to execute when a loop does break early?

  error_found = False

  for thing in list_of_things:
    if thing.error_condition:
      error_found = True
      break

  if error_found:
    pass


That makes sense on one level, but when you consider Pythons indenting rules, it just messes everything up.


I'm not sure how the indentation rules come into play here. I'm not saying that this:

  if blah:
    for .. in ..:
      ..
  else:
    ..
is visually confused with:

  for .. in ..:
    ..
  else:
    ..
What I'm saying is that for-else and while-else add syntax to the language that only solves a single specific instance of a class of similar issues, making it confusing.

Let's consider the issues that are in the same class:

- execute code block when loop condition isn't met the first time (i.e. loop never executes).

- execute code block when loop exits prematurely (e.g. break).

- execute code block when loop doesn't exit prematurely (e.g. no break) // execute code block when loop condition evals to false (first eval or any subsequent eval)

Only one of these issues is solved by the for-else/while-else syntax currently in Python, and using a generic keyword (else) just heightens the confusion (especially since this syntax differs from many other languages).

All of these cases may require the use of sentinels / additional checks to implement. Why is one specific instance any more relevant than the others to the point that it gets special treatment (i.e. special syntax in the core language)?


That logic definitely helps me think about it. But it does seem like a switch from the other wording that python uses. Feels like finally would be more intuitive, whereas without thinking about the search loop case else feels like something that would be triggered by a break or return. I wonder if using the feature would be more common if it was more intuitively worded.


It's analogous to the natural termination of a while-else, where the else block is executed after the condition evaluates to false:

    >>> a=1
    >>> while a>0:
        print(4)
        a=a-1
    else:
        print('done')
        
    4
    done
clarity cribbed from here: https://mail.python.org/pipermail/python-list/1999-July/0044...


Oh, that helps too -- it's an else clause for the conditional of the while loop :D


finally seems much more confusing to me -- the point of finally is that it will definitely be executed...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: