Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ethan Furman Newsgroups: comp.lang.python Subject: Re: for / while else doesn't make sense Date: Fri, 20 May 2016 16:58:14 -0700 Lines: 78 Message-ID: References: <573dfc0f$0$1586$c3e8da3$5496439d@news.astraweb.com> <573e54f7$0$1615$c3e8da3$5496439d@news.astraweb.com> <573FA496.5050708@stoneleaf.us> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de 7oj4O9EguMnXTeqiy8i2kwSjR7B9AhC4WIl5aA35aj/A== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'guido': 0.05; 'matches': 0.07; 'semantic': 0.07; 'expected.': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'okay': 0.09; 'runs,': 0.09; 'subject:while': 0.09; 'through,': 0.09; 'worse': 0.09; 'wrong,': 0.09; 'exception': 0.13; 'folks': 0.15; '2016': 0.16; 'certainly.': 0.16; 'confusion': 0.16; 'dwarfed': 0.16; 'empty"': 0.16; 'fallback': 0.16; 'iterable': 0.16; 'iterating': 0.16; 'loops': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:make': 0.16; 'syntactic': 0.16; 'value"': 0.16; 'wrote:': 0.16; 'case.': 0.18; 'say,': 0.18; 'runs': 0.18; '>>>': 0.20; 'people,': 0.20; 'machine': 0.21; 'pass': 0.22; 'am,': 0.23; 'bit': 0.23; '(or': 0.23; 'seems': 0.23; 'consistent': 0.23; 'words': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; 'rest': 0.26; 'fri,': 0.27; 'executing': 0.27; 'found.': 0.27; 'said,': 0.27; 'prints': 0.29; '~ethan~': 0.29; "i'm": 0.30; 'that.': 0.30; 'code': 0.30; 'posts': 0.30; 'guess': 0.31; 'certain': 0.31; 'post': 0.31; '"the': 0.32; 'maybe': 0.33; 'run': 0.33; 'problem': 0.33; 'usually': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'case,': 0.34; 'structure': 0.34; 'happened': 0.35; 'something': 0.35; 'expected': 0.35; "isn't": 0.35; 'step': 0.36; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'possible': 0.36; '(and': 0.36; 'keyword': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'being': 0.37; 'thought': 0.37; 'wanted': 0.37; 'thank': 0.38; 'means': 0.39; 'why': 0.39; "didn't": 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'called': 0.40; 'easy': 0.60; 'him': 0.60; 'your': 0.60; 'skip:u 10': 0.61; 'entire': 0.61; 'charset:windows-1252': 0.62; 'please,': 0.63; 'subject: / ': 0.63; 'more': 0.63; 'times': 0.63; 'our': 0.64; 'between': 0.65; 'picked': 0.66; 'situation': 0.67; 'sound': 0.72; 'obvious': 0.76; 'discount': 0.81; 'reasons:': 0.84; 'stars': 0.84; 'subject:else': 0.84; 'subject:sense': 0.84; 'dutch': 0.91; 'lucky': 0.93 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 In-Reply-To: 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: <573FA496.5050708@stoneleaf.us> X-Mailman-Original-References: <573dfc0f$0$1586$c3e8da3$5496439d@news.astraweb.com> <573e54f7$0$1615$c3e8da3$5496439d@news.astraweb.com> Xref: csiph.com comp.lang.python:108875 On 05/20/2016 04:55 AM, Jon Ribbens wrote: > On 2016-05-20, Steven D'Aprano wrote: >> On Fri, 20 May 2016 03:55 am, Jon Ribbens wrote: >>> I guess we should thank our lucky stars that you don't have a time >>> machine then, since that change would very much be one for the worse >>> in my opinion. for...else is perfectly straightforward and clearly >>> the right keywords to use. for...then would be entirely wrong. >> >> "Entirely" wrong? "Clearly" the right keyword? "Perfectly" straightforward? >> >> They are extremely strong words given the posts where *even the defenders* >> of "else" have admitted that it is a hard keyword to understand. But that's >> okay. Maybe you've thought of something the rest of us haven't, and have an >> entire consistent mental model of for...else that is easy to understand and >> makes it "perfectly straightforward and clearly the right keyword". >> >> Can you explain your model which makes "else" appropriate? > > Certainly. "else:" is (almost?) invariably used in the situation where > you are iterating through something in order to find a value which > matches a certain condition. So the "for:" block means "find this > value" and the "else:" means "else do this". I'm happy that you have a working mental model for for/else (seriously, I am) but please don't discount the confusion and consternation for the many folks who don't start with that mental model. The number of times I have /wanted/ to use the for/else structure for searching is small (and I remember them both ;) -- every other time what I wanted was an _else_ that ran iff the iterable was already empty when the _for_ encountered it. >> because like many people, my mental model was "the for block runs, OR ELSE >> the else block runs". This *incorrect* model seems like it works: if you >> set seq=[], say, it prints "seq is empty" as expected. > The problem there is that the mental model is *completely* wrong. D'oh. Completely wrong, but easy to guess because of the similarity with if/else. >> I never would have thought of that model if it had been called "then": >> >> for x in seq: >> pass >> then: >> print("executes after the for block completes") > > I would find that very confusing. "then:" makes it sound like > executing that block is the usual case, when in practice it is > usually the exception - the fallback code if the expected value > was not found. If you don't take the extra step of _break_ it is the usual case. Most of my for loops always run all the way through, so why have an else? Two possible reasons: - the loop didn't run at all - the loop didn't run all the way Guido picked the one that was obvious to him (him being Dutch and all ;) . I just read Nick's post about it, and while it helps, I think the syntactic similarity between try/else and for/else (did the block run without error) is dwarfed by the semantic difference: a try/else else block runs if nothing /bad/ happened whereas a for/else else block runs if something bad /did/ happen; to wit, the thing you were looking for was not found (or your loop didn't run at all ;) . But as others have said, this isn't going to change now, and I'm okay with that. But, please, be a bit more understanding of those who don't immediately grok the for/else and while/else loops. -- ~Ethan~