TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The optional “else” in Python loops

67 pointsby s16halmost 11 years ago

12 comments

Psyonicalmost 11 years ago
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.)
评论 #8051063 未加载
评论 #8051105 未加载
评论 #8051056 未加载
评论 #8051567 未加载
评论 #8051555 未加载
评论 #8051074 未加载
agroveralmost 11 years ago
Raymond Hettinger, one of the main Python devs, advises that the "else" in this situation should mentally be thought of as "notfound".
评论 #8051437 未加载
评论 #8051415 未加载
kazinatoralmost 11 years ago
This badly named else is reminiscent of the &quot;result forms&quot; in Lisp loops like `dolist`, `do` or `loop`.<p><pre><code> (loop for x below 10 finally (princ &quot;iterated over everything, &quot;) (princ &quot;indeed&quot;)) </code></pre> But, of course, these Lisp loops have a result value, and the result forms are an &quot;implicit progn&quot; which produces that value.
ddanddalmost 11 years ago
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&#x27;s go to statement isn&#x27;t so harmless as it adds complexity but doesn&#x27;t have big benefits.<p>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 <i>does</i> 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.)
gejjaxxitaalmost 11 years ago
I always strongly discourage the use of things like this construct in any language.<p>A language is meant to be read not just written. A non-expert python programmer who encounters this will be confused.
评论 #8051194 未加载
评论 #8051199 未加载
bashworkalmost 11 years ago
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:<p><a href="https://github.com/aosabook/500lines/blob/master/crawler/crawling.py#L100" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;aosabook&#x2F;500lines&#x2F;blob&#x2F;master&#x2F;crawler&#x2F;cra...</a>
评论 #8053415 未加载
gomesnayagamalmost 11 years ago
it clear to me that this notion is not maintainable at all after looking the comments though it is nice feature to have.
mdanielalmost 11 years ago
For me, the optional else on for loops isn&#x27;t nearly as mentally destabilizing as the optional else on try-except blocks. An ex-employee used to <i>love</i> doing that:<p><pre><code> try: None.doit() except Exception as e: logger.exception(&#x27;Nope&#x27;) else: # now what? pass</code></pre>
评论 #8051623 未加载
mhdalmost 11 years ago
Also known as &quot;Dijkstra&#x27;s loop&quot; to give credit where credit is due. Only other language I know that has it though is Oberon (and that just introduced it in a pretty <i>recent</i> update)
parhamalmost 11 years ago
Might be worth while looking at the video below for more Pythonic gems like this one.<p><a href="https://www.youtube.com/watch?v=OSGv2VnC0go" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=OSGv2VnC0go</a>
jw2013almost 11 years ago
This is all in Jeff Knupp&#x27;s Writing Idiomatic Python:<p><a href="http://www.jeffknupp.com/blog/2012/10/04/writing-idiomatic-python/" rel="nofollow">http:&#x2F;&#x2F;www.jeffknupp.com&#x2F;blog&#x2F;2012&#x2F;10&#x2F;04&#x2F;writing-idiomatic-p...</a><p>Many so-called Python Idioms are really non-intuitive and I don&#x27;t really appreciate.
评论 #8051543 未加载
enesunalalmost 11 years ago
I would choose `finally` keyword over the `else`.