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


Groups > comp.lang.python > #37344

Re: Understanding while...else...

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!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 <ethan@stoneleaf.us>
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; 'skip:/ 10': 0.07; 'python': 0.09; '(although': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'loop.': 0.09; 'message-id:@stoneleaf.us': 0.09; 'received:184.172': 0.09; 'received:gator410.hostgator.com': 0.09; 'terry': 0.09; 'useless': 0.09; '~ethan~': 0.09; 'constructs.': 0.16; 'received:69.93': 0.16; 'reedy': 0.16; 'sink': 0.16; 'wrote:': 0.17; 'example': 0.23; "python's": 0.23; 'statement': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'label': 0.27; 'trouble': 0.28; 'statements': 0.29; 'starts': 0.29; 'probably': 0.29; 'code': 0.31; 'point': 0.31; 'to:addr:python-list': 0.33; 'but': 0.36; "i'll": 0.36; 'why': 0.37; 'quite': 0.37; 'subject:: ': 0.38; 'several': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'skip:u 10': 0.60; 'real': 0.61; '(that': 0.62; 'subject:...': 0.63; 'times': 0.63; 'often,': 0.84
Date Tue, 22 Jan 2013 12:09:25 -0800
From Ethan Furman <ethan@stoneleaf.us>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2
MIME-Version 1.0
To python-list@python.org
Subject Re: Understanding while...else...
References <kdmj5n$me1$1@ger.gmane.org>
In-Reply-To <kdmj5n$me1$1@ger.gmane.org>
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - gator410.hostgator.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - stoneleaf.us
X-BWhitelist yes
X-Source
X-Source-Args
X-Source-Dir
X-Source-Sender ([173.12.184.235]) [173.12.184.235]:46840
X-Source-Auth ethan+stoneleaf.us
X-Email-Count 1
X-Source-Cap dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ==
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.833.1358885817.2939.python-list@python.org> (permalink)
Lines 40
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1358885817 news.xs4all.nl 6897 [2001:888:2000:d::a6]:46087
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:37344

Show key headers only | View raw


On 01/22/2013 09:44 AM, Terry Reedy wrote:
> Several people have trouble understanding Python's while-else and
> for-else constructs. It is actually quite simple if one starts with
> if-else, which few have any trouble with.
>
> Start with, for example
>
> if n > 0:
>    n -= 1
> else:
>    n = None
>
> The else clause is executed if and when the condition is false. (That
> the code is useless is not the point here.) Now use pseudo-Python label
> and goto statements to repeatedly decrement n
>
> label: check
> if n > 0:
>    n -= 1
>    goto: check
> else:
>    n = None
>
> 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. 
/Occasionally/ I'll actually have use for the search pattern, and then I 
can use the while- or for-else construct, but that's pretty rare for me.

~Ethan~

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


Thread

Re: Understanding while...else... Ethan Furman <ethan@stoneleaf.us> - 2013-01-22 12:09 -0800

csiph-web