Live data from Hacker News

The optional “else” in Python loops

shahriar.svbtle.com

41–50 of 50 posts

Re: The optional “else” in Python loops

#41
Not only it is not very intuitive, I also find it to hurt the linearity of code. The code which will be executed after the loop ends depends on how the loop ended. break statement is usually a glorified but mostly harmless go to, but I think Python's go to statement isn't so harmless as it adds complexity but doesn't have big benefits.

But, I do think that the else clause on try\except blocks is less harmful. It is less harmful because the control flow complexity of exceptions handling is already there on the catch clause, so while exceptions does add big control flow changes, the addition of the else clause causes a relative minor change on the linearity of code (as it is already broken by catch: clause.)

Re: The optional “else” in Python loops

#42

Earlier quoted context omitted.

finally blocks are always executed; this kind of else block is only executed if the for block exited normally (i.e. did not throw an exception).

How about a keyword such as "after"? I feel like that could have extensible potential in other areas of the language, and the name makes a little sense logically speaking.

or something like "then"?

Re: The optional “else” in Python loops

#43
post #10
post #2

While in theory this is useful, it's one of the least intuitive parts of python for me. I avoid them whenever possible because I feel "else" conveys the intent very poorly. (And I've been writing python for years.)

Raymond Hettinger has suggested that instead of `else:` the keyword should have been called `nobreak:` which conveys intentions better.

Or "unbroken", even... ;-)

Re: The optional “else” in Python loops

#45
post #25
post #2

While in theory this is useful, it's one of the least intuitive parts of python for me. I avoid them whenever possible because I feel "else" conveys the intent very poorly. (And I've been writing python for years.)

What's more, it dances very close to something that is a super-common error for novice programmers: the early return from a loop. I grade AP exams every year, and one of the hands-down most common conceptual mistakes I've seen (on problems where this is relevant) goes something like this: boolean lookForSomething(int parameter) { for (Item item: list) { if (item.matches(parameter)) return true; else return false; } }…

The 'else' after the 'if' is unnecessary, but so what? We're not running out of letters. IMO including an "else" makes it clearer.

Re: The optional “else” in Python loops

#46

Created an account to point out this gem by Guido himself that makes use of this and the fact that python variables are not scoped inside for loops: https://github.com/aosabook/500lines/blob/master/crawler/cra...

Oh my God! I would be so confused reading this code!

Re: The optional “else” in Python loops

#47
post #37
post #24

Earlier quoted context omitted.

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…

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)?

Re: The optional “else” in Python loops

#48
post #7

Earlier quoted context omitted.

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.

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.

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

Re: The optional “else” in Python loops

#49

Earlier quoted context omitted.

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
Post reply on HN