Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!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.043 X-Spam-Evidence: '*H*': 0.91; '*S*': 0.00; 'broke': 0.09; 'python': 0.11; 'erroneously': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'infinitely': 0.16; 'loops': 0.16; 'pythonic': 0.16; 'thereby': 0.16; 'throw': 0.16; 'worse.': 0.16; 'wrote:': 0.18; 'result.': 0.19; 'fit': 0.20; 'programming': 0.22; '31,': 0.24; '(or': 0.24; 'least': 0.26; 'code:': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'initialized': 0.31; 'this.': 0.32; 'fri,': 0.33; 'core': 0.34; 'agree': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'version': 0.36; 'should': 0.36; 'wrong': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; '30,': 0.65; 'here': 0.66; 'obvious': 0.74; 'points,': 0.84; 'rusi': 0.91; 'acknowledge': 0.93; 'taught': 0.96; '2013': 0.98 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:to :content-type; bh=W8VTtRbR42AXqORv0FtPzARNUwubQ2GxTDMdKKgva7A=; b=hmTAoDh0XKWigcykhb0sj9YyS7+ckz2c3kiGVVaCIimTuLFed5d20CSTq2bVmzocGa 59tDE/bMCrU0MNM04MrDtLA6Xrb3fsCIKf9xCyxvu//kw3RoUqxn8GpRkAzXz2bWoXf5 alEfhX7MVY18peyZkHrxcqGLZSPPx1bykG2hOmFLboxhPqPp8LvyqP6CWAGXtF4YnhoV Wd/AifA0JjWNyLpHzt9i9HjXhxLidOidyZwJecTtRq5V6ZlOkF/vaDngRjVHW3ta8Mui hzKxmaYDgj50nJN5se5E2G7yY1bVHH6tgU9Vc3NsuFcrI+MpwzUCxMHoGmpmH7VvJOdo tw2g== MIME-Version: 1.0 X-Received: by 10.220.111.133 with SMTP id s5mr6499965vcp.63.1369934591377; Thu, 30 May 2013 10:23:11 -0700 (PDT) In-Reply-To: <578a8e7f-4358-40ba-bcf8-d3a81b37c0d2@vy4g2000pbc.googlegroups.com> References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <51a6df59$0$11118$c3e8da3@news.astraweb.com> <578a8e7f-4358-40ba-bcf8-d3a81b37c0d2@vy4g2000pbc.googlegroups.com> Date: Fri, 31 May 2013 03:23:11 +1000 Subject: Re: Short-circuit Logic From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1369934594 news.xs4all.nl 16010 [2001:888:2000:d::a6]:45849 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:46515 On Fri, May 31, 2013 at 2:58 AM, rusi wrote: > On May 30, 5:58 pm, Chris Angelico wrote: >> The alternative would be an infinite number of iterations, which is far far worse. > > There was one heavyweight among programming teachers -- E.W. Dijkstra > -- who had some rather extreme views on this. > > He taught that when writing a loop of the form > > i = 0 > while i < n: > some code > i += 1 > > one should write the loop test as i != n rather than i < n, precisely > because if i got erroneously initialized to some value greater than n, > (and thereby broke the loop invariant), it would loop infinitely > rather than stop with a wrong result. And do you agree or disagree with him? :) I disagree with Dijkstra on a number of points, and this might be one of them. When you consider that the obvious Pythonic version of that code: for i in range(n,m): some code loops over nothing and does not go into an infinite loop (or throw an exception) when n >= m, you have to at least acknowledge that I'm in agreement with Python core code here :) That doesn't mean it's right, of course, but it's at least a viewpoint that someone has seen fit to enshrine in important core functionality. ChrisA