Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: "Sven R. Kunze" Newsgroups: comp.lang.python Subject: Re: empty clause of for loops Date: Thu, 17 Mar 2016 13:01:59 +0100 Lines: 44 Message-ID: References: <56E93413.6090108@mail.de> <56E94EB9.9050507@mail.de> <56E96DDA.30008@mail.de> <20160316100229.0ce28992@bigbox.christie.dr> <56E978FD.6070502@mail.de> <1458146254.105444.551062074.52B32ACC@webmail.messagingengine.com> <56E9902F.9080103@mail.de> <56E99159.70501@mail.de> <1458148134.113764.551096586.28A85FDB@webmail.messagingengine.com> <56e9fa05$0$22141$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de a8eY4hN/TDZavBLz2wCIJgGAe7GElTmlB7XvHt5IxatQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.015 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'else:': 0.03; 'chunk': 0.07; 'clause': 0.07; 'data:': 0.07; 'executed': 0.07; '(actually': 0.09; 'exception,': 0.09; '"not"': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'return,': 0.16; 'skips': 0.16; 'wrote:': 0.16; 'dropped': 0.22; 'header:In-Reply- To:1': 0.24; 'error': 0.27; 'fine': 0.28; 'raise': 0.29; 'code': 0.30; 'post': 0.31; "d'aprano": 0.33; 'jump': 0.33; 'raising': 0.33; 'steven': 0.33; 'true.': 0.33; 'received:10.0': 0.34; 'best,': 0.35; 'could': 0.35; 'should': 0.36; 'needed': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'thought': 0.37; 'several': 0.38; 'why': 0.39; "didn't": 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'called': 0.40; 'back': 0.62; 'charset:windows-1252': 0.62; 'introduction': 0.63; 'realise': 0.66; 'results': 0.66; 'finally': 0.70; 'heavy': 0.81; "'if": 0.84; 'break.': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=mail.de; s=mail201212; t=1458216118; bh=o6VIaYqQsRqGv1lp7JDcObZHpzL3LWnctY+v59j4VJA=; h=Subject:To:References:From:Date:In-Reply-To:From; b=bqL5WMZo2HaF0PqTd46wwhWGKDi4WsKtUfEX9QGU6H/s/w8qoIzRenlGiOQnHRrTg n5FFvNvVuByQ8lzahlVU6UN6NyIWuN2mGGmEG4dwaQv6SeA9a1HuJG7vPNIs2EVlX6 xuY+r2NTEJGcspUgBXHo16YWmRzzyF1FuapHlgQ0= In-Reply-To: <56e9fa05$0$22141$c3e8da3$5496439d@news.astraweb.com> X-purgate: clean X-purgate: This mail is considered clean (visit http://www.eleven.de for further information) X-purgate-type: clean X-purgate-Ad: Categorized by eleven eXpurgate (R) http://www.eleven.de X-purgate: This mail is considered clean (visit http://www.eleven.de for further information) X-purgate: clean X-purgate-size: 1374 X-purgate-ID: 154282::1458216118-000009F3-76E8EFFB/0/0 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:105087 On 17.03.2016 01:27, Steven D'Aprano wrote: > That post describes the motivating use-case for the introduction > of "if...else", and why break skips the "else" clause: > > > for x in data: > if meets_condition(x): > break > else: > # raise error or do additional processing > > > It might help to realise that the "else" clause is misnamed. It should be > called "then": > > for x in data: > block > then: > block > > > The "then" (actually "else") block is executed *after* the for-loop, unless > you jump out of that chunk of code by raising an exception, calling return, > or break. > > As a beginner, it took me years of misunderstanding before I finally > understood for...else and while...else, because I kept coming back to the > thought that the else block was executed if the for/while block *didn't* > execute. That's true. I needed to explain this to few people and I always need several attempts/starts to get it right in a simple statement: 'If you do a "break", then "else" is NOT executed.' I think the "NOT" results in heavy mental lifting. > I couldn't get code with for...else to work right and I didn't > understand why until finally the penny dropped and realised that "else" > should be called "then". That's actually a fine idea. One could even say: "finally". Best, Sven