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


Groups > comp.lang.python > #37515

Re: Understanding while...else...

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'clause': 0.07; 'executed': 0.07; 'false.': 0.07; 'see.': 0.07; 'python': 0.09; '(although': 0.09; 'loop.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'semantics': 0.09; 'terry': 0.09; 'benjamin': 0.16; 'blocks': 0.16; 'confusion': 0.16; 'explicitly.': 0.16; 'iterable': 0.16; 'iterable:': 0.16; 'iterated': 0.16; 'loops': 0.16; 'presume': 0.16; 're-written': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'saying.': 0.16; 'sink': 0.16; 'true:': 0.16; 'wrote:': 0.17; 'jan': 0.18; '>>>': 0.18; 'meant': 0.21; 'statement': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; '(which': 0.26; 'am,': 0.27; 'guess': 0.27; 'header:X-Complaints-To:1': 0.28; '>>>>': 0.29; 'behaviour': 0.29; 'once,': 0.29; 'probably': 0.29; 'this.': 0.29; 'stuff': 0.30; 'expect': 0.31; 'code': 0.31; 'computing': 0.32; 'could': 0.32; 'zero': 0.33; 'to:addr:python-list': 0.33; 'version': 0.34; 'false': 0.35; 'pm,': 0.35; 'there': 0.35; 'received:org': 0.36; 'really': 0.36; 'but': 0.36; 'thank': 0.36; 'execute': 0.37; 'two': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'think': 0.40; 'real': 0.61; 'side': 0.61; 'subject:...': 0.63; 'times': 0.63; 'more': 0.63; 'bothered': 0.65; '2013': 0.84; 'effects,': 0.84; 'furman': 0.84; 'often,': 0.84; 'oscar': 0.84; 'received:fios.verizon.net': 0.84; 'ethan': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Understanding while...else...
Date Wed, 23 Jan 2013 18:05:39 -0500
References <kdmj5n$me1$1@ger.gmane.org> <50FEF1F5.9050501@stoneleaf.us> <kdn84k$168$1@ger.gmane.org> <CAHVvXxSO93=FZ9jtbLZPihU_fZr_aXR58ifdCcsD3R-p4MnM1w@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-75-251-66.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0 Thunderbird/17.0
In-Reply-To <CAHVvXxSO93=FZ9jtbLZPihU_fZr_aXR58ifdCcsD3R-p4MnM1w@mail.gmail.com>
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.928.1358982375.2939.python-list@python.org> (permalink)
Lines 83
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1358982375 news.xs4all.nl 6985 [2001:888:2000:d::a6]:60911
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:37515

Show key headers only | View raw


On 1/22/2013 7:39 PM, Oscar Benjamin wrote:
> 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).

Thank you for the clarification.

If 'condition' has side effects, or if one is really bothered by 
computing it twice, Version 2 could be re-written as

if condition:
     while True:
         stuff()
         if not condition: break
else:
     other_stuff()

 > 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

I see. I guess people who want version 2 will have to write it explicitly.

-- 
Terry Jan Reedy

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


Thread

Re: Understanding while...else... Terry Reedy <tjreedy@udel.edu> - 2013-01-23 18:05 -0500

csiph-web