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


Groups > comp.lang.python > #37344 > unrolled thread

Re: Understanding while...else...

Started byEthan Furman <ethan@stoneleaf.us>
First post2013-01-22 12:09 -0800
Last post2013-01-22 12:09 -0800
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#37344 — Re: Understanding while...else...

FromEthan Furman <ethan@stoneleaf.us>
Date2013-01-22 12:09 -0800
SubjectRe: Understanding while...else...
Message-ID<mailman.833.1358885817.2939.python-list@python.org>
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~

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web