context managers and generators
Bob.Sidebotham at gmail.com
Bob.Sidebotham at gmail.com
Fri Jan 12 10:10:06 EST 2007
Jean-Paul Calderone wrote:
> On 12 Jan 2007 06:17:01 -0800, bob.sidebotham at gmail.com wrote:
> >I'm happily using context managers and co-routines, and would like to
> >use both at the same time, e.g.
>
> Python has generators, not co-routines.
They may be called generators, but I'm using them as co-routines.
> Wrap the generator in a function which co-operates with your context
> managers to clean-up or re-instate whatever they are interacting with
> whenever execution leaves or re-enters the generator.
I don't think wrapping the generator will do it. I think I would have
to wrap the actual yield call. Maybe that wouldn't be so bad.
There's not even any guarantee, by the way, that the yield even
returns: an exception outside the generator will not trigger the
exception in the context manager. In general, there is no hook (that I
can see) that helps with this.
I'm still thinking there is a better way to do it, and would appreciate
any ideas.
Bob
More information about the Python-list
mailing list