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


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

Re: Is this PEP-able? fwhile

Started byTerry Reedy <tjreedy@udel.edu>
First post2013-06-25 10:28 -0400
Last post2013-06-25 10:28 -0400
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: Is this PEP-able? fwhile Terry Reedy <tjreedy@udel.edu> - 2013-06-25 10:28 -0400

#49164 — Re: Is this PEP-able? fwhile

FromTerry Reedy <tjreedy@udel.edu>
Date2013-06-25 10:28 -0400
SubjectRe: Is this PEP-able? fwhile
Message-ID<mailman.3833.1372170531.3114.python-list@python.org>
On 6/25/2013 7:17 AM, jimjhb@aol.com wrote:

> for i in range(n) while safe(i): ..

Combined for-while and for-if statements have been proposed before and 
rejected. We cannot continuously add simple compositions to the langauge.

> I disagree. The problem IMO is that python 'for's are a different kind
> of 'for' in that they have no explicit indexing and no explicit range
> test; just a list which has elements drawn from it.  This is amazingly
> powerful and concise.  Unfortunately, the "breaks are just gotos"
> community often ruins this conciseness by going to 'while' or itertools
> (or worse) to avoid adding a break to a 'for' which needs to be
> terminated early.

'Break' and 'continue' were intended to be used ;-).

> I think suggestions like yours and Fabio's are good ones.  If 'for' has
> an 'else', why not a 'while'?

While-else and for-else follow from if-else. Which is to say, the else 
corresponds to the buried if that is part of while and for. The else 
part triggers when the if part is false. The difference from if-else is 
that the if part is tested multiple times.

while condition():
   block()
else:
   finish()

is equivalent to

while True:
   if condition():
     block()
     continue
   else:
     finish()
     break

-- 
Terry Jan Reedy

[toc] | [standalone]


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


csiph-web