interaction of 'with' and 'yield'

Jean-Paul Calderone exarkun at divmod.com
Tue Jul 31 13:44:42 CEST 2007


On Tue, 31 Jul 2007 07:34:09 -0400, Neal Becker <ndbecker2 at gmail.com> wrote:
>I'm wondering if a generator that is within a 'with' scope exits the 'with'
>when it encounters 'yield'.
>
>I would like to use a generator to implement RAII without having to
>syntactically enclose the code in the 'with' scope, and I am hoping that
>the the yield does not exit the 'with' scope and release the resource.

It doesn't.  Keep in mind that if the generator isn't resumed or garbage
collected, the cleanup with never run, though.

Jean-Paul



More information about the Python-list mailing list