for-else

Terry Reedy tjreedy at udel.edu
Mon Mar 10 23:34:56 EDT 2008


"Carl Banks" <pavlovevidence at gmail.com> wrote in message 
news:dfcf19e3-a216-461d-998e-30fbe139a722 at 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







More information about the Python-list mailing list