[Python-Dev] PEP 340: Breaking out.

Nicolas Fleury nidoizo at yahoo.com
Fri May 6 11:09:39 CEST 2005


Paul Moore wrote:
> On 5/5/05, Nick Coghlan <ncoghlan at gmail.com> wrote:
>>   2. Manual protocol implementations are _significantly_ easier to write
> 
> Hmm, I've not tried so I'll have to take your word for this. But I
> don't imagine writing manual implementations much - one of the key
> features I like about Guido's proposal is that generators can be used,
> and the implementation is a clear template, with "yield" acting as a
> "put the block here" marker (yes, I know that's an
> oversimplification!).

If using a generator is easier to code (but I tend to agree with Nick), 
a new type, a one-shot-generator (not really a generator, but some type 
of continuation), as suggested Steven Bethard with stmt, could be created:

     def opening(filename, mode="r"):
         f = open(filename, mode)
         try:
             yield break f
         finally:
             f.close()

I prefer Nick's proposal however, since it simplifies non-looping 
constructs (no generator-template, break of parent loop supported), 
while leaving looping constructs (a minority in IMO) possible using a 
for, making things even clearer to me (but harder to implement).  I'm 
still not convinced at all that using generators to implement a 
acquire/release pattern is a good idea...

Regards,
Nicolas



More information about the Python-Dev mailing list