{"id":3884,"date":"2011-10-19T14:00:16","date_gmt":"2011-10-19T19:00:16","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=3884"},"modified":"2011-10-19T14:00:19","modified_gmt":"2011-10-19T19:00:19","slug":"pythons-forelse","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2011\/10\/19\/pythons-forelse\/","title":{"rendered":"Python&rsquo;s for\/else"},"content":{"rendered":"<p><a href=\"http:\/\/nedbatchelder.com\/blog\/201110\/forelse.html\">Ned Batchelder<\/a>:<\/p>\n<blockquote cite=\"http:\/\/nedbatchelder.com\/blog\/201110\/forelse.html\"><p>As the loop executes, the &ldquo;if&rdquo; becomes like a giant if\/elif\/elif ladder: each time around the loop adds another test of the condition, but only if all previous tests were false. The &ldquo;else&rdquo; clause caps it all off, just as it would in an explicit if\/elif\/else structure. And it works just like the explicit structure: if all of the conditions are false, then the &ldquo;else&rdquo; clause is run.<\/p><\/blockquote>\n<p>Even though I understand the problem this is trying to solve, I have never found it intuitive. I still have to think when reading or writing one of these loops. It just doesn&rsquo;t seem worth it for such a small gain.<\/p>","protected":false},"excerpt":{"rendered":"<p>Ned Batchelder: As the loop executes, the &ldquo;if&rdquo; becomes like a giant if\/elif\/elif ladder: each time around the loop adds another test of the condition, but only if all previous tests were false. The &ldquo;else&rdquo; clause caps it all off, just as it would in an explicit if\/elif\/else structure. And it works just like the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-3884","post","type-post","status-publish","format-standard","hentry","category-programming-category"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/3884","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=3884"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/3884\/revisions"}],"predecessor-version":[{"id":3885,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/3884\/revisions\/3885"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=3884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=3884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=3884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}