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.)
The optional “else” in Python loops
41–50 of 50 posts
Re: The optional “else” in Python loops
#42Earlier 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.
Re: The optional “else” in Python loops
#43While 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.
Re: The optional “else” in Python loops
#44Re: The optional “else” in Python loops
#45While 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; } }…
Re: The optional “else” in Python loops
#46Created 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...
Re: The optional “else” in Python loops
#47Earlier 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.
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
#48Earlier 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.
Re: The optional “else” in Python loops
#49Earlier 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...