[Python-Dev] PEP 340: Breaking out.

Nicolas Fleury nidoizo at yahoo.com
Wed May 4 00:29:33 CEST 2005


Guido van Rossum wrote:
> [Skip Montanaro]
>>    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.
> 
>>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.

I would expect programmers to do more than only replace existing 
try/finally blocks.  The support for RAII patterns in Python might 
result in more use of RAII primitives and some may fit very well inside 
a loop.  It might not be a bad idea to look at what other languages are 
doing with RAII.  Also, even if there's no occurence right now in the 
standard library, it doesn't mean it has always been the case in the 
code evolution, where debugging such pitfall would not be cool.

FWIW, I expect most generators used in block-syntax to not be loops. 
What would imply to support these to pass "break" to parent loop at 
run-time?  Maybe generators are not the way to go, but could be 
supported natively by providing a __block__ function, very similarly to 
sequences providing an __iter__ function for for-loops?

We could avoid explaining to a newbie why the following code doesn't 
work if "opening" could be implemented in way that it works.

for filename in filenames:
     block opening(filename) as file:
         if someReason: break

By the way, FWIW, my preference if to have no keyword, making it clearer 
that some block statements are loops and others not, but probably 
amplifying the "break" problem.

Regards,
Nicolas



More information about the Python-Dev mailing list