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":"想想也是,讀過一些源代碼,也沒見過這樣的寫法呀^_^"}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章