[Python-ideas] With clauses for generator expressions
Andrew Barnert
abarnert at yahoo.com
Thu Nov 15 12:37:32 CET 2012
> From: Masklinn <masklinn at masklinn.net>
> Sent: Thu, November 15, 2012 2:25:47 AM
>
> On 2012-11-15, at 11:08 , Andrew Barnert wrote:
> > This is an almost-unrelated side issue. A generator used in a single thread
> > defines a fully deterministic dynamic scope
>
> I think you meant "a context manager" not "a generator"
No, I meant a generator. "As long as the generator has values to generate, and
has not been closed or destroyed" is a dynamic scope. "Until the end of this
with statement block" is a static scope. The only reason the context managers in
both your example and mine have dynamic scope is because they're embedded in
generators.
> and my example
> quite clearly demonstrates that the interaction between context managers
> and generators completely break context managers as dynamic scopes.
No it doesn't. It demonstrates that it's possible to create indeterminate
scopes, and context managers cannot help you if you do so. "Until the client
exhausts the iterator, given that the client is not going to exhaust the
iterator" effectively means "Until the client goes away". Which means you need a
context manager around the client. The fact that you don't have one means that
your client is inherently broken. You'll have the exact same problems with a
trivial local object (e.g., its __del__ method won't get called by PyPy).
However, if the client *did* have a context manager (or exhausted, closed, or
explicitly deleted the generator), a properly-written generator would clean
itself up, while a naively-written one would not. That's what I meant by
"properly-written". Not that it's guaranteed to clean up even when used by a
broken client, because that is completely impossible for any object (generator
or otherwise), but that it is guaranteed to clean up when used by a
properly-written client.
More information about the Python-ideas
mailing list