Working around a lack of 'goto' in python

Georgy no.mail at
Mon Mar 8 10:09:22 CET 2004

"Roger Binns" <rogerb at> wrote in message news:vpbth1-drb.ln1 at
| Here is how you conventionally write it without continue 2
| for line in lines:
|      chars=line
|      sawspecial=False
|      for z in chars:
|          if z=='!':
|              sawspecial=True
|              break
|      if not sawspecial:
|          print chars # and other work
| Note how at the important point you have to write 'break', despite
| thinking 'continue' in your head.  You have to introduce a new
| state variable.  As I said, this gets a lot worse if there are
| multiple conditions in which you want to break/continue multiple
| levels (more variables, more 'continues' where you think 'break'
| and vice versa).
| Roger

I guess 'for' with 'else' part could help here, but I personally never
remembered to which condition is that 'else' part, because for me
'for' is not a condition but a loop. Maybe other people can write
the solution with 'for-else'. Anyway it won't be clearer to the reader,
than simplier schemes with plain for-break-continue and try-catch.


More information about the Python-list mailing list