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

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

问题描述

许多Python程序员可能不知道循环的和循环的的语法包含一个可选的<$ c
$ b $ $ $ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ )
其他:
clean_up()

else 子句是某些类型的清理操作的好地方,并且在正常的循环终止时执行:即,退出循环 return break 跳过 else 子句;在 continue 执行后退出。我知道这只是因为我只是查阅 (又一次),因为当执行 else 子句时,我永远不会记得



始终?在循环的失败,顾名思义?定期终止?即使循环退出 return ?我不能完全确信没有查找它。



我怪我对关键字的选择持续的不确定性:我找到 else 这个语义非常难以理解。我的问题不是为什么这个关键字用于这个目的(我可能会投票结束,虽然只是在阅读答案和意见后),但我怎么能想到 else 关键字,这样它的语义就有意义了,我可以记住它了吗?



我确定有相当数量的我可以想象,为了与 try 语句的 else:子句保持一致,我也必须查找),并且不添加到Python保留字列表的目标。也许选择 else 的理由将会澄清它的功能并使其更加令人难忘,但是我将名字连接到功能之后,而不是在历史解释本身之后。



这个问题的答案,我的问题被简单地关闭为一个重复的,包含了很多有趣的后台故事。我的问题有一个不同的焦点(如何连接 else 的特定语义与关键字选择),但我觉得应该有一个链接到这个问题的地方。 >

解决方案

(这是灵感来自@Mark Tolonen的回答。)

如果语句的条件计算结果为false,则运行它的 else 子句。
同样, while循环运行else子句,如果它的条件计算结果为false



这个规则匹配你描述的行为:


  • 在正常的执行过程中,while循环重复运行,直到条件计算结果为false,因此自然退出循环运行else子句。
  • 当你执行一个 break 语句时,你不需要评估条件就退出循环,所以条件不能计算为false,也不能执行else子句。
  • 执行 continue 语句时,可以再次评估条件,做循环迭代开始时的正常工作。
    因此,如果条件为真,则保持循环,但如果为假,则运行else子句。
  • 退出循环的其他方法,如 return ,不要评估条件,因此不要运行else子句。


for 循环的行为方式相同。如果迭代器有更多的元素,只要考虑条件为真,否则为假。


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

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.

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.

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?

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.

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.

解决方案

(This is inspired by @Mark Tolonen's answer.)

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:

  • 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 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天全站免登陆