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

Hung Jung Lu hungjunglu at yahoo.com
Thu Oct 23 10:19:16 EDT 2003


jjl at pobox.com (John J. Lee) wrote in message news:<87ekx5rvk2.fsf at pobox.com>...
> def next_foo(foo):
>     return foo**2
> 
> while condition(foo):
>     ...
>     if hmm():
>         foo = next_foo()
>         continue
>     ...
>     if hmph(): break
>     ...
>     if hrm():
>         foo = next_foo()
>         continue
>     ....
>     foo = next_foo()
> 
> Yuck!

Exception handling is often used for this purpose. (Common practice in
Python, believe it or not.)

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

regards,

Hung Jung




More information about the Python-list mailing list