Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail 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; 'python,': 0.02; '"this': 0.03; '16,': 0.03; 'model,': 0.05; 'subject:Python': 0.06; 'function:': 0.09; 'structure,': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'question.': 0.14; '"if"': 0.16; 'flush': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'it".': 0.16; 'left,': 0.16; 'left:': 0.16; 'loop.': 0.16; 'occurence': 0.16; 'readability': 0.16; 'rem': 0.16; 'true:': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'unlike': 0.19; 'written': 0.21; '>>>': 0.22; 'separate': 0.22; 'cc:addr:python.org': 0.22; 'helper': 0.24; 'fine': 0.24; 'regardless': 0.24; 'cc:2**0': 0.24; 'options': 0.25; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; "doesn't": 0.30; '(like': 0.30; 'label': 0.30; 'message- id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; "i'm": 0.30; 'code': 0.31; 'lines': 0.31; "d'aprano": 0.31; 'indentation': 0.31; 'languages:': 0.31; 'steven': 0.31; 'this.': 0.32; 'critical': 0.32; 'languages': 0.32; 'sense': 0.34; 'maybe': 0.34; 'could': 0.34; 'subject: (': 0.35; 'common': 0.35; 'something': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'yield': 0.36; 'similar': 0.36; 'half': 0.37; 'subject:New': 0.37; 'represent': 0.38; 'follows:': 0.38; 'pm,': 0.38; 'does': 0.39; 'bad': 0.39; 'structure': 0.39; 'support,': 0.39; 'sure': 0.39; 'how': 0.40; 'consists': 0.60; 'middle': 0.60; 'most': 0.60; 'break': 0.61; 'full': 0.61; 'new': 0.61; 'simply': 0.61; 'such': 0.63; 'more': 0.64; 'bottom': 0.67; 'body.': 0.68; 'line,': 0.68; 'physical': 0.72; 'introduce': 0.78; '2015': 0.84; 'condition.': 0.84; 'goto': 0.84; 'instantly': 0.84; 'pardon': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=ajJg/CpcqTA7q9zdeGT81xmR265Jjnfki+7l0J1YbhI=; b=jkHE+cJvZgDrHlYkOoOOF3PIuGdSZxA0AavTWrpxiuvVo323XnzrNCgkARCTs4iE6s tBjevz+4k15+/QXZlLufzXRZsIoehP6YuiwMxDymwZVeHUKqIkvqeqi5tfnF2YQI2SNv S+hlF54fFgrNkL0pSii3Qd0igeYxPL3NrKEI159njoDigTGz5fg6WxTojPEtrs0nYxcU rqkJhvMd8c/hQ2InUQdIE24VSeycbLv2mkKihPkWxs3q6QDOo/xdunAwzLfwb6tGgbv3 ifK830xO8hoeC1Yo68eABCxFSUHCEBpoGxoS8Bu2MCLjx3j080RnXX4PnUBaai3o61nM nGKg== MIME-Version: 1.0 X-Received: by 10.107.134.206 with SMTP id q75mr41769717ioi.27.1429191683006; Thu, 16 Apr 2015 06:41:23 -0700 (PDT) In-Reply-To: <552F97EA.5080401@rece.vub.ac.be> References: <9fc57fc9-0399-4ff3-882a-d041f02827d8@googlegroups.com> <552F772B.8030201@rece.vub.ac.be> <552f9264$0$11092$c3e8da3@news.astraweb.com> <552F97EA.5080401@rece.vub.ac.be> Date: Thu, 16 Apr 2015 23:41:22 +1000 Subject: Re: New to Python - block grouping (spaces) From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 126 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1429191686 news.xs4all.nl 2912 [2001:888:2000:d::a6]:37561 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89023 On Thu, Apr 16, 2015 at 9:07 PM, Antoon Pardon wrote: > On 04/16/2015 12:43 PM, Steven D'Aprano wrote: >> On Thursday 16 April 2015 20:09, Antoon Pardon wrote: >> >>> I beg to differ. The most common occurence is a loop with a break >>> condition in the middle I would prefer such a loop to be written as >>> follows: >>> >>> repeat: >>> some >>> code >>> break_when condition: >>> more >>> code The case of a loop structure with its condition in the middle is one that few languages support, so the physical structure has to be something like: goto middle while not condition: more code label middle some code or while True: some code if condition: break more code or maybe some code while not condition: more code some code But I'm not sure how you could represent this more appropriately, regardless of your indentation. Unindenting an "if" in the middle of a loop doesn't instantly scream "this is the loop header". Using a goto to jump half way into a loop is a really REALLY bad idea in most programs (and it's illegal in lots of languages anyway). Repeating the setup code is fine if it's a single line, but not else. Similar to this is the capturing condition. Say you want to process lines until you get to one that consists solely of a full stop. In C, you can do this (kinda); in Pike, where strings are first-class objects (like Python, unlike C), you can definitely do it, syntactically: while ((line=get_next_line()) != ".") process_line(line); Perfectly legal. Not perfectly readable. In Python, your options are a helper generator function: def next_line(): while True: line = get_next_line() if line==".": break yield line for line in next_line(): process_line(line) or a loop with a critical line of code duplicated above and at the bottom of the loop (risks 'continue' failure): line = get_next_line() while line!=".": process_line(line) line = get_next_line() or the "mid-loop break" model, which is what makes this similar to the above: while True: line = get_next_line() if line==".": break process_line(line) There's no nice way to spell "grab this and retain it". But again, I'm not sure how a change of indentation could improve this. >> That structure makes no sense to me. Why is the "break_when" *outside* of >> the loop? Why does the "break_when condition" introduce a new block? > > How do you mean outside the loop? Do you consider the "else" outside the > if statement? The "break_when" is part of the loop structure, not the loop body. With an entry-checked condition, the loop structure is flush left, and the loop body is indented: x, y = 0, 1 while x < y: x = (x + y) / 2 y = f(x) Some languages have a similar construct for an exit-checked condition, eg REXX: do until x > y /* as above */ end or BASIC-derived languages: do rem as above loop while x < y In all three cases, the condition is on a line that's not indented. So logically, the mid-checked loop could also go flush left: do some code while condition more code end Whether this actually improves readability or not is a separate question. The "break_when" isn't so much *outside* the loop as simply *not inside* the loop. ChrisA