如何理解 Python 循环的“else"子句? [英] How can I make sense of the `else` clause of Python loops?

查看:21
本文介绍了如何理解 Python 循环的“else"子句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多 Python 程序员可能不知道 while 循环和 for 循环的语法包含一个可选的 else: 子句:

Many Python programmers are probably unaware that the syntax of while loops and for loops includes an optional else: clause:

for val in iterable:
    do_something(val)
else:
    clean_up()

else 子句的主体是某些类型的清理操作的好地方,并在循环正常终止时执行:即,使用 return<退出循环/code> 或 break 跳过 else 子句;continue 执行后退出.我知道这只是因为我只是查了一下(又一次),因为我永远不记得 什么时候 else 子句被执行了.

The body of the else clause is a good place for certain kinds of clean-up actions, and is executed on normal termination of the loop: I.e., exiting the loop with return or break skips the else clause; exiting after a continue executes it. I know this only because I just looked it up (yet again), because I can never remember when the else clause is executed.

总是?关于失败"循环,顾名思义?定期终止?即使循环以 return 退出?如果不查一下,我永远无法完全确定.

Always? On "failure" of the loop, as the name suggests? On regular termination? Even if the loop is exited with return? I can never be entirely sure without looking it up.

我将持续的不确定性归咎于关键字的选择:我发现 else 对于这种语义非常难以记忆.我的问题不是为什么将此关键字用于此目的";(我可能会投票关闭,尽管只有在阅读了答案和评论之后),但是我如何考虑 else 关键字以便其语义有意义,因此我可以记住

I blame my persisting uncertainty on the choice of keyword: I find else incredibly unmnemonic for this semantics. My question is not "why is this keyword used for this purpose" (which I would probably vote to close, though only after reading the answers and comments), but how can I think about the else keyword so that its semantics make sense, and I can therefore remember it?

我确信对此进行了大量讨论,我可以想象做出选择是为了与 try 语句的 else: 子句(我还必须查找),并且目标是不添加到 Python 的保留字列表中.也许选择 else 的原因会澄清它的功能并使它更容易记住,但我是在将名称与功能联系起来,而不是在历史解释本身之后.

I'm sure there was a fair amount of discussion about this, and I can imagine that the choice was made for consistency with the try statement's else: clause (which I also have to look up), and with the goal of not adding to the list of Python's reserved words. Perhaps the reasons for choosing else will clarify its function and make it more memorable, but I'm after connecting name to function, not after historical explanation per se.

这个问题的答案,我的问题被简单地关闭作为重复,包含很多有趣的回故事.我的问题有不同的侧重点(如何将 else 的特定语义与关键字选择联系起来),但我觉得应该在某处提供指向此问题的链接.

The answers to this question, which my question was briefly closed as a duplicate of, contain a lot of interesting back story. My question has a different focus (how to connect the specific semantics of else with the keyword choice), but I feel there should be a link to this question somewhere.

推荐答案

if 语句在其条件计算结果为假时运行其 else 子句.同样,如果 while 循环的条件计算结果为假,则它会运行 else 子句.

An if statement runs its else clause if its condition evaluates to false. Identically, a while loop runs the else clause if its condition evaluates to false.

此规则与您描述的行为相符:

This rule matches the behavior you described:

  • 在正常执行中,while 循环会反复运行,直到条件的计算结果为 false,因此自然退出循环会运行 else 子句.
  • 当你执行一个 break 语句时,你退出循环而不评估条件,所以条件不能评估为假并且你永远不会运行 else 子句.
  • 当您执行 continue 语句时,您会再次评估条件,并完全按照您通常在循环迭代开始时所做的操作.因此,如果条件为真,则继续循环,但如果为假,则运行 else 子句.
  • 其他退出循环的方法,例如 return,不评估条件,因此不运行 else 子句.
  • In normal execution, the while loop repeatedly runs until the condition evaluates to false, and therefore naturally exiting the loop runs the else clause.
  • When you execute a break statement, you exit out of the loop without evaluating the condition, so the condition cannot evaluate to false and you never run the else clause.
  • When you execute a continue statement, you evaluate the condition again, and do exactly what you normally would at the beginning of a loop iteration. So, if the condition is true, you keep looping, but if it is false you run the else clause.
  • Other methods of exiting the loop, such as return, do not evaluate the condition and therefore do not run the else clause.

for 循环的行为方式相同.如果迭代器有更多元素,则将条件视为真,否则视为假.

for loops behave the same way. Just consider the condition as true if the iterator has more elements, or false otherwise.

这篇关于如何理解 Python 循环的“else"子句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆