[Python-Dev] PEP 340 -- concept clarification

Nick Coghlan ncoghlan at gmail.com
Wed May 4 14:32:01 CEST 2005

Delaney, Timothy C (Timothy) wrote:
> Guido van Rossum wrote:
>>I'd like the block statement to be defined exclusively in terms of
>>__exit__() though.
> This does actually suggest something to me (note - just a thought - no
> real idea if it's got any merit).
> Are there any use cases proposed for the block-statement (excluding the
> for-loop) that do *not* involve resource cleanup (i.e. need an
> __exit__)?
> This could be the distinguishing feature between for-loops and
> block-statements:
> 1. If an iterator declares __exit__, it cannot be used in a for-loop.
>    For-loops do not guarantee resource cleanup.
> 2. If an iterator does not declare __exit__, it cannot be used in a
> block-statement.
>    Block-statements guarantee resource cleanup.
> This gives separation of API (and thus purpose) whilst maintaining the
> simplicity of the concept. Unfortunately, generators then become a pain
> :( We would need additional syntax to declare that a generator was a
> block generator.

Ah, someone else did post this idea first :)

To deal with the generator issue, one option would be to follow up on Phillip's 
idea of a decorator to convert a generator (or perhaps any standard iterator) 
into a block iterator.

I think this would also do wonders for emphasising the difference between for 
loops and block statements.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-Dev mailing list