Python equivalent for perl's "next"-statement?

John J. Lee jjl at pobox.com
Thu Oct 23 14:03:01 EDT 2003


hungjunglu at yahoo.com (Hung Jung Lu) writes:

> jjl at pobox.com (John J. Lee) wrote in message news:<87ekx5rvk2.fsf at pobox.com>...
[...]
> Exception handling is often used for this purpose. (Common practice in
> Python, believe it or not.)

I believe you :-)


> class Next(Exception): pass
> 
> while condition(foo):
>     try:
>         ...
>         if hmm(): raise Next
>         ...
>         if hmph(): break
>         ...
>         if hrm(): raise Next
>         ...
>     except Next: pass
>     foo = foo**2

Yes, that's certainly much better, thanks for pointing that out.  Even
better with the except: pass replaced with a finally:.

I still don't particularly like the gratuitous extra indentation
level, though, and I think continue: is more explicit (which was the
justification of finally:, of course).


John




More information about the Python-list mailing list