[Python-Dev] Re: anonymous blocks

Reinhold Birkenfeld reinhold-birkenfeld-nospam at wolke7.net
Fri Apr 22 19:20:46 CEST 2005

Nick Coghlan wrote:

> Interestingly, with this approach, "for dummy in my_resource()" would still wrap 
> the block of code in the entrance/exit code (because my_resource *is* a 
> generator), but it wouldn't get the try/finally semantics.
> An alternative would be to replace the 'yield None' with a 'break' or 
> 'continue', and create an object which supports the resource protocol and NOT 
> the iterator protocol. Something like:
> def my_resource():
>    print "Hi!"   # Do entrance code
>    continue      # Go on with the contents of the 'with' block
>    print "Bye!"  # Do exit code
> (This is currently a SyntaxError, so it isn't ambiguous in any way)

Oh, it is ambiguous, as soon as you insert a for/while statement in your resource
function and want to call continue in there. Other than that, it's very neat.

Maybe "yield" alone (which is always a SyntaxError) could be used.


Mail address is perfectly valid!

More information about the Python-Dev mailing list