Working around a lack of 'goto' in python
Georgy
no.mail at available.net
Mon Mar 8 04:09:22 EST 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:
| 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.
Georgy
More information about the Python-list
mailing list