[Python-Dev] Re: anonymous blocks

Josiah Carlson jcarlson at uci.edu
Wed Apr 27 21:21:25 CEST 2005


Guido van Rossum <gvanrossum at gmail.com> wrote:
> Ouch. Another bug in the PEP. It was late. ;-)
> 
> The "finally:" should have been "except StopIteration:" I've updated
> the PEP online.
> 
> > Unless it is too early for me, I believe what you wanted is...
> > 
> >         itr = iter(EXPR1)
> >         arg = None
> >         while True:
> >             VAR1 = next(itr, arg)
> >             arg = None
> >             BLOCK1
> >         else:
> >             BLOCK2
> 
> No, this would just propagate the StopIteration when next() raises it.
> StopIteration is not caught implicitly except around the next() call
> made by the for-loop control code.

Still no good.  On break, the else isn't executed.

How about...

        itr = iter(EXPR1)
        arg = None
        while True:
            try:
                VAR1 = next(itr, arg)
            except StopIteration:
                BLOCK2
                break
            arg = None
            BLOCK1

 - Josiah



More information about the Python-Dev mailing list