[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: for-else

Terry Reedy

3/8/2008 9:16:00 PM


"egbert" <egbert.bouwman@hccnet.nl> wrote in message
news:20080308152034.GA12009@hccnet.nl...
| However the loop-else really works more like this:
| . try to do the loop;
| . if it starts but is interrupted by a break,
| . then do something else as well.

This is NOT how loop-else works for Python.
If you want to do something special on breaks,
put the break-only code before the break.

while loop_condition:
<loop statements>
if break_condition:
<break-only statements>
break
<more loop stuff>

| So they are completely different beasts, and if you try to use
| or explain the one according to the rules of the other one,
| you put a serious strain on your synapses.

I did not mean to broke your brain.

| The explanation that the if-else and the loop-else
| follow the same pattern, runs more or less like this:
| . all conditions to run the loop to its completion were met,
| . which means that the loop-condition is not met (any more),
| . which means that we must do something else.
| For me that is orwellian logic: success is failure.

I gave a clear and coherent explanation of how while derives from if,
and correspondingly, how while-else derives from if-else, to help those who
want to read and write Python code. Building on the pseudo-snippet above,
one can write

while loop_condition:
<loop statements>
if break_condition:
<break-only statements>
break
<more loop stuff>
else:
<completion-only statements>

Python allows one to have both break-only and completion-only sections
together in one compound statement and *without* having to fiddle with a
special flag variable. I am sorry if you cannot appreciate such elegance
and can only spit on it as 'orwellian'.

If the sense of else were reversed, one would have to write the clumbsier

complete = True # though false at this point
while loop_condition:
<loop statements>
if break_condition:
complete = False
break
<more loop stuff>
else:
<break-only statements>
if complete:
<completion-only statements>

Terry Jan Reedy



4 Answers

Carl Banks

3/10/2008 2:40:00 PM

0

On Mar 8, 5:15 pm, "Terry Reedy" <tjre...@udel.edu> wrote:
> I gave a clear and coherent explanation of how while derives from if,
> and correspondingly, how while-else derives from if-else, to help those who
> want to read and write Python code. Building on the pseudo-snippet above,
> one can write
>
> while loop_condition:
> <loop statements>
> if break_condition:
> <break-only statements>
> break
> <more loop stuff>
> else:
> <completion-only statements>
>
> Python allows one to have both break-only and completion-only sections
> together in one compound statement and *without* having to fiddle with a
> special flag variable. I am sorry if you cannot appreciate such elegance
> and can only spit on it as 'orwellian'.


Just to play Devil's advocate, there is one draw drawback to "such
elegance": when there are multiple break statements. Which means
you'd have to duplicate the break-only condition, or refactor somehow
(which may or may not be suitable).

There have been a couple occasions where I felt the best solution was
to a temporary varible like so:

completed = False
while loop_condition:
<loop statements>
if break_condition:
break
<more loop stuff>
if some_other_break_condition:
break
<more loop stuff>
else:
completed = False
if not completed:
<break-only statements>

It felt icky but we're all still here so it couldn't have been that
bad.


Carl Banks

rockingred

3/10/2008 2:44:00 PM

0

On Mar 8, 4:15 pm, "Terry Reedy" <tjre...@udel.edu> wrote:
> "egbert" <egbert.bouw...@hccnet.nl> wrote in message
>
> news:20080308152034.GA12009@hccnet.nl...
> | However the loop-else really works more like this:
> | .  try to do the loop;
> | .  if it starts but is interrupted by a break,
> | .  then do something else as well.
>
> This is NOT how loop-else works for Python.
> If you want to do something special on breaks,
> put the break-only code before the break.
>
> while loop_condition:
>   <loop statements>
>   if break_condition:
>     <break-only statements>
>     break
>   <more loop stuff>
>
> | So they are completely different beasts, and if you try to use
> | or explain the one according to the rules of the other one,
> | you put a serious strain on your synapses.
>
> I did not mean to broke your brain.
>
> | The explanation that the if-else and the loop-else
> | follow the same pattern, runs more or less like this:
> | .  all conditions to run the loop to its completion were met,
> | .  which means that the loop-condition is not met (any more),
> | .  which means that we must do something else.
> | For me that is orwellian logic: success is failure.
>
> I gave a clear and coherent explanation of how while derives from if,
> and correspondingly, how while-else derives from if-else, to help those who
> want to read and write Python code.  Building on the pseudo-snippet above,
> one can write
>
> while loop_condition:
>   <loop statements>
>   if break_condition:
>     <break-only statements>
>     break
>   <more loop stuff>
> else:
>   <completion-only statements>
>
> Python allows one to have both break-only and completion-only sections
> together in one compound statement and *without* having to fiddle with a
> special flag variable.  I am sorry if you cannot appreciate such elegance
> and can only spit on it as 'orwellian'.
>
> If the sense of else were reversed, one would have to write the clumbsier
>
> complete = True # though false at this point
> while loop_condition:
>   <loop statements>
>   if break_condition:
>     complete = False
>     break
>   <more loop stuff>
> else:
>   <break-only statements>
> if complete:
>   <completion-only statements>
>
> Terry Jan Reedy

Terry, instead of using "complete = True" and setting it to false on
failure, why not set "loop_completed = False" and set it to True if
the break condition is met?

Terry Reedy

3/11/2008 3:23:00 AM

0


"rockingred" <willsteve2003@yahoo.ca> wrote in message
news:7a6555fa-819a-41c0-befe-ee0123272b31@59g2000hsb.googlegroups.com...
On Mar 8, 4:15 pm, "Terry Reedy" <tjre...@udel.edu> wrote:
> If the sense of else were reversed, one would have to write the clumbsier
>
> complete = True # though false at this point
> while loop_condition:
> <loop statements>
> if break_condition:
> complete = False
> break
> <more loop stuff>
> else:
> <break-only statements>
> if complete:
> <completion-only statements>
>
> Terry Jan Reedy

Terry, instead of using "complete = True" and setting it to false on
failure, why not set "loop_completed = False" and set it to True if
the break condition is met?
[OE not quoting properly]
=====================
Because completion is False when broken? Actually, I am not sure what you
mean without seeing the snippet rewritten. Certainly, one could set
'broken=False' at top (tho not true) and 'broken = True' before breaking
and test for 'not broken' at end, but that is not an improvement.

tjr



Terry Reedy

3/11/2008 3:35:00 AM

0


"Carl Banks" <pavlovevidence@gmail.com> wrote in message
news:dfcf19e3-a216-461d-998e-30fbe139a722@n75g2000hsh.googlegroups.com...
| Just to play Devil's advocate, there is one draw drawback to "such
| elegance": when there are multiple break statements. Which means
| you'd have to duplicate the break-only condition, or refactor somehow
| (which may or may not be suitable).

Yes, I knowingly glided over the possibility of multiple break statements
with common break-only code and no completion-only code (other than
artifactual flag setting as in your code below ;-). I presume that that
triple condition is even rarer than simpler situations where one of those
conditions is false.

| There have been a couple occasions where I felt the best solution was
| to a temporary varible like so:

But I will accept your testimony that the set of such use cases is not
empty.

| completed = False
| while loop_condition:
| <loop statements>
| if break_condition:
| break
| <more loop stuff>
| if some_other_break_condition:
| break
| <more loop stuff>
| else:
| completed = False

I presume you meant True

| if not completed:
| <break-only statements>
|
| It felt icky but we're all still here so it couldn't have been that
| bad.

tjr