Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: for / while else doesn't make sense Date: Thu, 19 May 2016 14:11:42 -0600 Lines: 29 Message-ID: References: <573dfc0f$0$1586$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de wN3tX2+jO5X6XYB+Q+h8IgIyKI7iyF/UrlzmuaNdhEyw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'finally:': 0.05; 'executed': 0.07; 'exit': 0.07; 'exception:': 0.09; 'executes': 0.09; 'subject:while': 0.09; 'example:': 0.10; 'received:209.85.218': 0.10; 'things.': 0.15; 'thu,': 0.15; '*almost*': 0.16; '2016': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'run.': 0.16; 'subject:make': 0.16; 'wrote:': 0.16; 'case.': 0.18; 'try:': 0.18; 'am,': 0.23; 'passing': 0.23; 'header:In-Reply-To:1': 0.24; 'all.': 0.24; 'chris': 0.26; 'fri,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'idea': 0.28; 'guarantees': 0.29; 'loop,': 0.29; 'that.': 0.30; 'code': 0.30; '[1]': 0.32; 'run': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'except': 0.34; 'gets': 0.35; 'received:google.com': 0.35; 'execution': 0.35; 'received:209.85': 0.36; 'possible': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:209': 0.38; 'to:addr:python.org': 0.40; 'still': 0.40; 'chance': 0.60; 'matter': 0.63; 'subject: / ': 0.63; '20,': 0.66; 'guaranteed': 0.67; 'finally': 0.70; 'power': 0.72; 'stronger': 0.84; 'subject:else': 0.84; 'subject:sense': 0.84; 'to:name:python': 0.84; 'absolutely': 0.88 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=Gmxb4Xk1eP8y8SJREOAZNMSHszCWKrJEYysShpJCxzE=; b=I5dGeJHTNvIFvj3IVAGFU/KINTzdgeDNVmvhVLI9fQHv3KL733YCfhsPezcXyIrjTN Ttp9abDjadiUbmEDKPiZSVL0YN1YRSHMBinsDBfjWDrqZcIiwikbM3jUkTtpptJZCjDP 3FxRSb5t9Muoqtle8OPp+7T4QgT9g/ZKcAcv7mxnTGwWr0ao9E0+tsAh0NCh5v5FfvbO O8z9jVC/ZxBfIT+iVlJV0snJcslKjvE+WYv7dDXIjufM+gVkL5CZF0QBKVlD22UjkfsJ 6qxWO+V4Wzjso2R4sV2prma2JgkHTwwCUEEQSHZQrhIPVzgG3E/hTbra8SqDfp0pV3Ke AgQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=Gmxb4Xk1eP8y8SJREOAZNMSHszCWKrJEYysShpJCxzE=; b=FLOOHLxddCyGKlUnqioK7TmvTw/8BaiwDAUMHXoJW2mAUcyz/uxqWfSxC4XSGegx/2 BV9w/+wvKLQefDJQIXm5vlleFFth7qjNPMUL8HtrXiGL1iPtYw6QGA681Hffg2OT61jS bfbBy9OetTnvlOdMTJdMhMJvC40Pwri80W8Lij70beGseis4yxwBP6vb3G/N4pvJec+H g/ftlwf6qb1s7ezlVPTsMcHDlMr+tyasjyTJX4dcO3ikP3x6lEtoLmy7owebcjbn9Z01 HmDDYAZhV66A7KTL7zct3M/kSjzP+jgWuM/uiNx04bl3gkfULP9ARaZLBo5lSFs8d09V mgVA== X-Gm-Message-State: AOPr4FW2yuOLwAqMtCyasHrsAVsPYFpNNmol5anjPcY1oVKw7t7eLNPwhGwva/zoAUg+59zVnyOsLUPVaApEVQ== X-Received: by 10.202.214.208 with SMTP id n199mr9386407oig.144.1463688741496; Thu, 19 May 2016 13:12:21 -0700 (PDT) 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: X-Mailman-Original-References: <573dfc0f$0$1586$c3e8da3$5496439d@news.astraweb.com> Xref: csiph.com comp.lang.python:108841 On Thu, May 19, 2016 at 2:01 PM, Chris Angelico wrote: > On Fri, May 20, 2016 at 3:46 AM, Steven D'Aprano wrote: >> The idea of finally is >> that it executes no matter what happens[1]. >> >> [1] Well, *almost* no matter what. If you pull the power from the computer, >> the finally block never gets a chance to run. > > Nor if you kill -9 the process, or get into an infinite loop, or any > number of other things. Specifically, what the finally block > guarantees is that it will be executed *before any code following the > try block*. In this example: > > try: > code1 > except Exception: > code2 > else: > code3 > finally: > code4 > code5 > > Once you hit code1, you are absolutely guaranteed that code5 *will > not* be run prior to code4. The guarantee is stronger than that. It's possible to exit the try block without passing execution to code5 at all. The finally block is still guaranteed to be executed in this case.