[Python-Dev] PEP 340: Breaking out.

Guido van Rossum gvanrossum at gmail.com
Tue May 3 20:31:55 CEST 2005


[Skip Montanaro]
>     >> Yeah, but "block synchronized(v1)" doesn't look like a loop.  I think
>     >> this might be a common stumbling block for people using this
>     >> construct.
> 
>     Guido> How many try/finally statements have you written inside a loop?
>     Guido> In my experience this is extreeeemely rare. I found no
>     Guido> occurrences in the standard library.

[Skip again]
> How'd we start talking about try/finally?

Because it provides by far the dominant use case for 'block'. The
block-statement is intended to replace many boilerplace uses of
try/finally. In addition, it's also a coroutine invocation primitive.

> To the casual observer, this
> looks like "break" should break out of the loop:
> 
>     while True:
>         block synchronized(v1):
>             ...
>             if v1.field:
>                 break
>         time.sleep(1)

Without 'block' this would be written as try/finally. And my point is
that people just don't write try/finally inside a while loop very
often (I found *no* examples in the entire standard library).

> The PEP says:
> 
>     Note that it is left in the middle whether a block-statement
>     represents a loop or not; this is up to the iterator, but in the
>     most common case BLOCK1 is executed exactly once.
> 
> That suggests to me it's still not clear if the block statement is actually
> a looping statement.  If not, then "break" should almost certainly break out
> of the while loop.

Dynamically, it's most likely not a loop. But the compiler doesn't
know that, so the compiler considers it a loop.

> BTW, what did you mean by "left in the middle" mean?  I interpreted it as
> "still undecided", but it's an idiom I've never seen.  Perhaps it should be
> replaced by something more clear.

It may be a Dutch phrase that doesn't translate to English as wel as I
thought. It doesn't exactly mean "still undecided" but more "depends
on your POV". I'll use something different, and also clarify that as
far as break/continue are concerned, it *is* a loop.

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list