Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Victor Savu Newsgroups: comp.lang.python Subject: Re: "for/while ... break(by any means) ... else" make sense? Date: Wed, 29 Jun 2016 10:01:03 +0000 Lines: 30 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de ST2SHF9I+9QOR4KRhYUBlQP6QByOLQqaxbl5ZCRCpQSg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.022 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'clause': 0.07; 'naturally': 0.09; 'subject:while': 0.09; 'argument': 0.15; "'except'": 0.16; 'cab': 0.16; 'clause.': 0.16; 'closure.': 0.16; 'iterator': 0.16; 'iterator.': 0.16; 'jumps': 0.16; 'keyword.': 0.16; 'looping': 0.16; 'rationale': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'skips': 0.16; 'statement.': 0.16; 'subject:break': 0.16; 'subject:make': 0.16; 'used:': 0.16; '(in': 0.18; 'language': 0.19; 'assuming': 0.22; 'level,': 0.22; 'trying': 0.22; 'replacing': 0.23; 'discussion': 0.24; 'sense': 0.26; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; 'actual': 0.28; 'search.': 0.29; 'subject:/': 0.30; 'posts': 0.30; 'statement': 0.32; 'getting': 0.33; 'except': 0.34; 'gets': 0.35; 'gives': 0.35; 'received:google.com': 0.35; 'behind': 0.35; 'best,': 0.35; 'something': 0.35; 'but': 0.36; 'there': 0.36; 'keyword': 0.36; 'subject:" ': 0.36; 'totally': 0.36; 'to:addr :python-list': 0.36; 'subject:?': 0.36; 'thought': 0.37; 'goes': 0.39; 'well.': 0.40; 'to:addr:python.org': 0.40; 'mark': 0.40; 'some': 0.40; 'save': 0.60; 'decision': 0.61; 'hope': 0.61; 'body': 0.61; 'entire': 0.61; 'for:': 0.64; 'between': 0.65; 'else.': 0.66; 'reuse': 0.66; 'statement,': 0.66; 'here': 0.66; 'opinions': 0.71; "'for'": 0.84; "'then'": 0.84; 'subject:any': 0.84; 'subject:else': 0.84; 'subject:sense': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to; bh=XCj4hm2tpd7GxkVDW1+KqgvKMkiHlmOmGl9LYioTzVc=; b=QillM7qm2D7BXCN2GVzjlgncg9q+54wwvHuQtO9W1WH5iehxoPNTfQ6NUsdnq5UISe 3Zn9JFkRZCunHCvLsf5bM0cVF1L5xcsn0qhb4o+Dg5qs+Z1w3MR4ylFNqF7c78EBiOAN DQv4xRzeK8+fIiVMRR+HGojJ0k4ZsO5QtPemA3OyvnQORODS9sS2D1MMMYKJGfdrHUvv YbPFcekVWkBdkaHAdZ1tGyf94Z4xu28iHCpNZHPZAW68gI7g6uex1ZwhM7uwfPV0MiFH iG012Q1emVTV9Dookh+tIsMNKhDxh5hrnUxt97MNsd64Zt18xHoqKAHfTGLlcVKBciQM Q1NQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=XCj4hm2tpd7GxkVDW1+KqgvKMkiHlmOmGl9LYioTzVc=; b=Cp3DJJE+39LbR+fb9LQPDlNi7QL4ZFiq+n7+HAOlBSVptUblELyVrGRAIlXc+obEyv 50BdS1GZ/L94X02W6vWWa5b8hnI/KS9aSAtUpKM1NuJXo7RInsIvqiX7OC6YUca/qb3K ZRrQQrhgGTRKWcTQgZo6+Bj1Jf65YILj4HYlhquOxT/jb/hvQ3tGiVE45MIWDOzN5y6v F50fbXCm+IkdM4jU7a8v5x3X4arRhz0bA+SNfopAAP0uSEM7uObFxN+YmGCSWF0Hq7MO Vlc1APebWEq6cf2v++LMdSihGtSQgCCYSvircbFdl8Aqv8V1MKO/GTReKLYGyR38/tlG SiaQ== X-Gm-Message-State: ALyK8tIDDEQoybtm5GHmznMhx9yqz3+YP/oNReEw9iOtU9R3RVs2CtmrMvmHfu/+zaWJAvv7XgHj0vc+NGadEQ== X-Received: by 10.28.167.69 with SMTP id q66mr8418750wme.100.1467194473108; Wed, 29 Jun 2016 03:01:13 -0700 (PDT) X-Content-Filtered-By: Mailman/MimeDel 2.1.22 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: Xref: csiph.com comp.lang.python:110765 There are many posts trying to explain the else after for or while. Here is my take on it: There are three ways of getting out of a (for/while) loop: throw, break or the iterator gets exhausted. The question is, how cab we tell which way we exited? For the throw, we have the except clause. This leaves us to differentiatr between break and normal exhaustion of the iterator. This is that the else clause is for: we enter the body iff the loop iterator was exhausted. A lot of discussion goes around the actual keyword used: else. Opinions may differ, but I for one would have chosen 'then' as a keyword to mark something that naturally happens as part of the for statement but after the looping is over; assuming break jumps out of the entire statement, it makes sense that it skips the 'then' body as well. (In the same way, I prefer 'catch' to 'except' as a correspondent to 'throw', but all of this is just bikeshedding). At a language design level, the decision was made to reuse one of the existing keywords and for better or worse, 'else' was chosen, which can be thought of as having no relation to the other use of the same keyword in the 'if' statement. The only rationale behind this was to save one keyword. The search analogy often used for justifying 'else' is (to me) totally bogus, since the same argument can be used to support replacing the keyword 'for' by the keyword 'find' and have looping only as a side-effect of a search. I hope this gives you some sense of closure. Best, VS