Python3 for ... else ...陷阱

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"举个例子"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"假设有如下代码:"}]},{"type":"codeblock","attrs":{"lang":"python"},"content":[{"type":"text","text":"for i in range(10):\n if i == 5:\n print 'found it! i = %s' % i\nelse:\n print 'not found it ...'"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我们期望的结果是,当找到5时打印出:"}]},{"type":"codeblock","attrs":{"lang":"python"},"content":[{"type":"text","text":"found it! i = 5"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"实际上打印出来的结果为:"}]},{"type":"codeblock","attrs":{"lang":"python"},"content":[{"type":"text","text":"found it! i = 5\nnot found it ..."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"显然这不是我们期望的结果。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"解答"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"根据官方文档说法:"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"When the items are exhausted (which is immediately when the sequence is empty), the suite in the else clause, if present, is executed, and the loop terminates."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"A "},{"type":"text","marks":[{"type":"italic"},{"type":"underline"},{"type":"strong"}],"text":"break"},{"type":"text","marks":[{"type":"strong"}],"text":" statement executed in the first suite terminates the loop without executing the else clause’s suite. A "},{"type":"text","marks":[{"type":"italic"},{"type":"underline"},{"type":"strong"}],"text":"continue"},{"type":"text","marks":[{"type":"strong"}],"text":" statement executed in the first suite skips the rest of the suite and continues with the next item, or with the else clause if there was no next item."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://docs.python.org/2/reference/compound_stmts.html#the-for-statement","title":"https://docs.python.org/2/reference/compound_stmts.html#the-for-statement"},"content":[{"type":"text","text":"https://docs.python.org/2/reference/compound_stmts.html#the-for-statement"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"大意是说"},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"strong"}],"text":"当迭代的对象迭代完并为空时,位于else的子句将执行"}]},{"type":"text","marks":[{"type":"strong"}],"text":","},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"strong"}],"text":"而如果在for循环中含有break时则直接终止循环,并不会执行else子句"}],"marks":[{"type":"strong"}]},{"type":"text","text":"。"},{"type":"text","marks":[{"type":"strong"}],"text":"同样的原理适用于"},{"type":"codeinline","content":[{"type":"text","text":"while ... else"}],"marks":[{"type":"strong"}]},{"type":"text","marks":[{"type":"strong"}],"text":"循环。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所以正确的写法应该为:"}]},{"type":"codeblock","attrs":{"lang":"python"},"content":[{"type":"text","text":"for i in range(10):\n if i == 5:\n print 'found it! i = %s' % i\n break\nelse:\n print 'not found it ...'"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"假如你使用了pylint检测代码,会提示:"}]},{"type":"codeblock","attrs":{"lang":"python"},"content":[{"type":"text","text":"Else clause on loop without a break statement (useless-else-on-loop)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所以养成使用pylint检测代码的习惯还是很有必要的,像这种逻辑错误比较难以发现。但最重要的还是要\b熟悉文档,\b甚至源码,追踪溯源,从根本上定位以及解决问题。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其实,在不少书籍中提到,"},{"type":"text","marks":[{"type":"italic"},{"type":"strong"}],"text":"不建议在for和while循环后面使用else语句块!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"想想也是,读过一些源代码,也没见过这样的写法呀^_^"}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章