Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #37386

Re: Understanding while...else...

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!weretis.net!feeder4.news.weretis.net!news.unit0.net!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <oscar.j.benjamin@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'clause': 0.07; 'executed': 0.07; 'false.': 0.07; 'python': 0.09; '(although': 0.09; 'loop.': 0.09; 'semantics': 0.09; 'terry': 0.09; 'cc:addr:python-list': 0.10; 'blocks': 0.16; 'cc:name:python list': 0.16; 'confusion': 0.16; 'iterable': 0.16; 'iterable:': 0.16; 'iterated': 0.16; 'loops': 0.16; 'presume': 0.16; 'reedy': 0.16; 'saying.': 0.16; 'sink': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'meant': 0.21; 'cc:2**0': 0.23; 'statement': 0.23; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; '(which': 0.26; 'am,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'behaviour': 0.29; 'once,': 0.29; 'probably': 0.29; 'this.': 0.29; 'stuff': 0.30; 'expect': 0.31; 'code': 0.31; 'zero': 0.33; 'version': 0.34; 'received:google.com': 0.34; 'false': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'execute': 0.37; 'two': 0.37; 'why': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'where': 0.40; 'think': 0.40; 'real': 0.61; 'subject:...': 0.63; 'times': 0.63; 'more': 0.63; '2013': 0.84; 'furman': 0.84; 'often,': 0.84; 'oscar': 0.84; 'ethan': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=ItFgj6dxxUZmrnHSPyAcqvXZqgP+lRHW/xb+lyXCn5o=; b=LLoKCdHvYLoio8rL0tu0osbdfqKcqvyzmNVD3n0/gN4vaR+bqzDLgT/M7kWVCRcSQ8 f8o3ipNHrdDQW19OFygIJeAiO4O3OUs713k/DrAAyDkVy4Ca4DyI+jujKiagqIODJUOh 6ORTn4UCouRTJbYR2hTwYLRFvK0Hx1b900KDbx2z/BAjuvktRHrwmqTH/m5Epi3r0leb CZrmnXtK1N7HF77/3yR/PmtBtyS6NrBr/RVmZVgpLQpCgXp68FoTtWW/nWCsKIffu210 djTByGUD9oErUrXRqbRdqo3owqNAjteOsT+gf0iT518Iwj7ehW6DvDZp5y/S1gN6Jon8 DRZg==
MIME-Version 1.0
X-Received by 10.112.102.5 with SMTP id fk5mr10061566lbb.31.1358901579458; Tue, 22 Jan 2013 16:39:39 -0800 (PST)
In-Reply-To <kdn84k$168$1@ger.gmane.org>
References <kdmj5n$me1$1@ger.gmane.org> <50FEF1F5.9050501@stoneleaf.us> <kdn84k$168$1@ger.gmane.org>
Date Wed, 23 Jan 2013 00:39:39 +0000
Subject Re: Understanding while...else...
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
To Terry Reedy <tjreedy@udel.edu>
Content-Type text/plain; charset=ISO-8859-1
Cc Python List <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.849.1358901582.2939.python-list@python.org> (permalink)
Lines 65
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1358901582 news.xs4all.nl 6878 [2001:888:2000:d::a6]:46236
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:37386

Show key headers only | View raw


On 22 January 2013 23:41, Terry Reedy <tjreedy@udel.edu> wrote:
> On 1/22/2013 3:09 PM, Ethan Furman wrote:
>>
>> On 01/22/2013 09:44 AM, Terry Reedy wrote:
>>>
[SNIP]
>>> The else clause is executed if and when the condition is false.
>>> Now use a real Python while statement to do the *same
>>> thing*.
>>>
>>> while n > 0:
>>>    n -= 1
>>> else:
>>>    n = None
>>
>>
>> I understand how it works (although it did take a while for it to sink
>> in); my gripe, and probably why it is misunderstood so often, is that
>> nine times out of ten when I /want/ to use a while-else or for-else I
>> only want the true/false check /once/, at the beginning of the loop.
>
>
> I do not understand what you are saying. There already is only one
> true/false check, at the beginning of the loop. If you only want the check
> *performed* once, you would use if-else. But I presume you know this.

I think he meant that he would use the else clause more often if it
had the semantics so that the two blocks below were equivalent:

# Version 1
while condition:
    # stuff
else:
    # other stuff

# Version 2
if condition:
    while condition:
        # stuff
else:
    # other stuff

So he wants a convenient way to execute code only if the loop
performed zero iterations. I think that often when people are confused
about the else clause on while loops it is because they expect this
behaviour (which would also be useful). The same confusion arises with
for loops where people expect the else clause to execute if the
iterable was empty so that these would be equivalent:

# Version 1
for x in iterable:
    # stuff
else:
    # other stuff

# Version 2
iterated = False
for x in iterable:
    iterated = True
    # stuff
if not iterated:
    # other stuff


Oscar

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Understanding while...else... Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-01-23 00:39 +0000

csiph-web