Is this PEP-able? fwhile

Terry Reedy tjreedy at udel.edu
Tue Jun 25 10:28:35 EDT 2013


On 6/25/2013 7:17 AM, jimjhb at 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




More information about the Python-list mailing list