Working around a lack of 'goto' in python
no.mail at available.net
Mon Mar 8 10:09:22 CET 2004
"Roger Binns" <rogerb at rogerbinns.com> wrote in message news:vpbth1-drb.ln1 at home.rogerbinns.com...
| Here is how you conventionally write it without continue 2
| for line in lines:
| for z in chars:
| if z=='!':
| 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).
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