Singleton generators as code blocks

Dave Benjamin ramen at lackingtalent.com
Sat Nov 22 02:18:32 EST 2003


In article <mailman.982.1069465581.702.python-list at python.org>, Jp Calderone wrote:
>> Has anyone used this sort of "singleton generator" technique?
> 
>   This is a pretty nifty approach, but it seems to have (at least?) one serious
> drawback.  If the body of the loop raises an exception, the generator is
> never resume, and the cleanup (or whatever) is never run.

Yeah, that is indeed a problem. That was in the back of my mind when I wrote
that disclaimer (that it's just an example), but you summed it up very well.

>   A way to reformulate the above without losing its conciseness eludes me
> currently.

Me too. The obvious solution would be to wrap the body in a try/finally, but
then, I guess we're right back where we started. ;)

Thanks for the feedback.

-- 
.:[ dave benjamin (ramenboy) -:- www.ramenfest.com -:- www.3dex.com ]:.
: d r i n k i n g   l i f e   o u t   o f   t h e   c o n t a i n e r :




More information about the Python-list mailing list