Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Michael Selik Newsgroups: comp.lang.python Subject: Re: for / while else doesn't make sense Date: Tue, 14 Jun 2016 04:37:31 +0000 Lines: 72 Message-ID: References: <9e7b6ded-15ad-490a-9842-b3da5d8a048d@googlegroups.com> <575d3430$0$11108$c3e8da3@news.astraweb.com> <575e167a$0$1608$c3e8da3$5496439d@news.astraweb.com> <575f6f58$0$1591$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de XYbGbW1tLLNsMJrl/FGsNArivGDkFv0qmBX2zcgmRSfg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'model,': 0.05; 'that?': 0.05; 'clause': 0.07; 'correct.': 0.07; 'exception.': 0.07; 'executed': 0.07; 'false.': 0.07; 'imply': 0.07; 'purpose.': 0.07; '"if': 0.09; 'executes': 0.09; 'implies': 0.09; 'part,': 0.09; 'statements': 0.09; 'subject:while': 0.09; 'terminates': 0.09; 'python': 0.10; ':-)': 0.12; 'explicitly': 0.15; '2016': 0.16; '``return``': 0.16; 'clause.': 0.16; 'command,': 0.16; 'conditional': 0.16; 'fire,': 0.16; 'guilty': 0.16; 'lying,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:make': 0.16; 'to:addr:pearwood.info': 0.16; "to:name:steven d'aprano": 0.16; 'wrote:': 0.16; '<': 0.18; 'say,': 0.18; 'skip': 0.18; 'written,': 0.18; '>': 0.18; 'to:2**1': 0.21; 'not,': 0.22; 'saying': 0.22; 'context.': 0.22; 'explicit': 0.22; 'latter': 0.22; 'logical': 0.22; 'occurs': 0.22; 'am,': 0.23; 'bit': 0.23; 'wrote': 0.23; 'matching': 0.23; 'written': 0.24; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; "doesn't": 0.26; 'message-id:@mail.gmail.com': 0.27; 'executing': 0.27; 'function': 0.28; 'skip:u 20': 0.28; '13,': 0.29; 'catching': 0.29; 'cpu': 0.29; 'code': 0.30; 'regardless': 0.31; 'statement': 0.32; 'run': 0.33; 'url:python': 0.33; 'michael': 0.33; 'usually': 0.33; 'clarify': 0.33; "d'aprano": 0.33; 'jump': 0.33; 'steven': 0.33; 'true.': 0.33; 'tue,': 0.34; 'running': 0.34; 'list': 0.34; 'skip:& 20': 0.35; 'received:google.com': 0.35; 'execution': 0.35; 'returning': 0.35; 'comment': 0.35; 'but': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'skip:& 10': 0.37; '12,': 0.37; 'experience,': 0.38; 'received:209': 0.38; 'skip:s 40': 0.38; 'several': 0.38; 'to:addr:python.org': 0.40; 'some': 0.40; 'url:3': 0.60; 'skip:u 10': 0.61; 'avoid': 0.61; 'entire': 0.61; 'no.': 0.62; 'here.': 0.62; 'subject: / ': 0.63; 'you.': 0.64; 'statement,': 0.66; 'fact,': 0.67; 'completing': 0.72; 'power': 0.72; '3.6': 0.84; '6:11': 0.84; 'claim.': 0.84; 'earth.': 0.84; 'killing': 0.84; 'speech': 0.84; 'subject:else': 0.84; 'subject:sense': 0.84; '"one': 0.91; 'misleading': 0.91; 'url:reference': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=I8QcRbbEkAuZsOJ6bfjAxd9z29YXPZCxf+5T8F9tlN4=; b=yO40WoojlGVf2EhoD5jssVMiLtnhs8ZkGlvPwUuCw7zXMqXMuC2NQz9YXU1YMu3Q4u xFbNA9SjqTt0bakXsAXXSNXuBvbN8jht4vczpxX+tEks2hkYJdVI+PDrZptTO3aWHfnR 2cu2APJq9zoXfRMzc34wQlQFMyBGfYfWhfImiR8jDNjB5EVUZYEqG20tyquf0zbIbuKS Ze/+//hXMeS0sY0g8kjj4Guju6FOE73LozSXyUwyzvraePrUOKy0VAJliFiJNgq3Lfp1 9px/28DnCYqGn2RklTYJ6gmf6fjON6RqUIQ9tFg3BVEbK3FkKOII9AdP7znXM59/Xoaw rYMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=I8QcRbbEkAuZsOJ6bfjAxd9z29YXPZCxf+5T8F9tlN4=; b=ir8hdvxzGgdukY3anf3SsrROYNiSvnDaY+yZOibsNfq4abEYLXhSeghN6Qlkim1oDv kxBF9ct8m/ZkUYbH7yeDi/cqRuzNnIaui9qAuUHjbz8lqW8GTaBHuF3lVsutDlgEfBD8 GUNdamydBzlikycT/Sjm0QXbKC4q0DBUDi6WbjQi44GwbEUc+J/KIES2t5g0wSff9XEu 14o/oxE6D1k51n0TO/5VpeyB09FLUJpxpiLfopclsAMBSRH5JsVwMTEhrwJF1xCeFMdT ju87bj0FUZ8+ebzSstHrMbCP9v/ZJNQKnuCHwRTcq+IegxhLgtsmhOXsV5jr0xOw7Zgw eUGA== X-Gm-Message-State: ALyK8tLtR/Bg/gv+TW7oNhYgoh0/hElajoOKcoZwU1vuRyFOV6y9g6vVrzpmN6xSrva38OgyNhM0ZpP7SrCbMQ== X-Received: by 10.140.41.164 with SMTP id z33mr7301930qgz.54.1465879060312; Mon, 13 Jun 2016 21:37:40 -0700 (PDT) In-Reply-To: <575f6f58$0$1591$c3e8da3$5496439d@news.astraweb.com> 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: X-Mailman-Original-References: <9e7b6ded-15ad-490a-9842-b3da5d8a048d@googlegroups.com> <575d3430$0$11108$c3e8da3@news.astraweb.com> <575e167a$0$1608$c3e8da3$5496439d@news.astraweb.com> <575f6f58$0$1591$c3e8da3$5496439d@news.astraweb.com> Xref: csiph.com comp.lang.python:109925 On Mon, Jun 13, 2016 at 10:46 PM Steven D'Aprano wrote: > On Tue, 14 Jun 2016 09:45 am, Michael Selik wrote: > > On Sun, Jun 12, 2016 at 10:16 PM Steven D'Aprano > > wrote: > >> On Mon, 13 Jun 2016 04:44 am, Michael Selik wrote: > >> > On Sun, Jun 12, 2016 at 6:11 AM Steven D'Aprano < > >> > steve+comp.lang.python@pearwood.info> wrote: > >> > > >> >> - run the for block > >> >> - THEN unconditionally run the "else" block > >> >> > >> > Saying "unconditionally" is a bit misleading here. As you say, it's > >> > conditioned on completing the loop without break/return/raise. > >> > >> It's also conditional on the OS not killing the Python process, > >> conditional on the CPU not catching fire, conditional on the user not > >> turning the power of, and conditional on the sun not exploding and > >> disintegrating the entire earth. > >> > >> In the absence of any event which interferes with the normal execution > of > >> code by the Python VM, and in the absence of one of a very few > >> explicit "JUMP" statements which explicitly jump out of the compound > >> for...else statement, the else clause is unconditionally executed afte= r > >> the for clause. > > > Saying that ``raise``, ``break``, and ``return`` are "one of a very few > > explicit JUMP statements" implies that they are obscure. > > What? No. How do you get that? > The context. That was right after a list of several (mostly) oddball situations. In my experience, that pattern of speech is usually used to imply that everything listed is also exceptionally strange. There's a simple mental model, one which has the advantage of actually > matching the implementation: for...else executes else unconditionally, ..= . > If Python 3.6 introduces a GOTO command, then my mental model of > for...else doesn't need to change. > That's a good explanation. The documentation says, "A break statement executed in the first suite terminates the loop without executing the else clause=E2=80=99s suite." Do you think that qualification of "without execut= ing the else clause" is unnecessary/redundant? Regardless of the implementation, I think that explanation -- if break, then skip the else-clause -- helps clarify the purpose. https://docs.python.org/3/reference/compound_stmts.html#the-for-statement else: # only if no break occurs > and then I would feel guilty for lying, because that comment is not, in > fact, correct. Returning out of the function from inside the loop will al= so > avoid running the else part, as will an exception. > I absolve you of guilt! :-) If you had written, "guaranteed to run if no break," then lawyers will come after you. If you had written "if and only if" some mathematicians might complain. As you wrote it, it's actually true= . An "if and only if" logical statement has two parts: "if no break, run else-clause" and "if break, do not run else-clause". As you say, the first part is false. But you only made the latter claim.