
On Mon, Aug 28, 2017 at 6:56 PM, Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:
Yury Selivanov wrote:
I saying that the following should not work:
def nested_gen(): set_some_context() yield
def gen(): # some_context is not set yield from nested_gen() # use some_context ???
And I'm saying it *should* work, otherwise it breaks one of the fundamental principles on which yield-from is based, namely that 'yield from foo()' should behave as far as possible as a generator equivalent of a plain function call.
Consider the following generator: def gen(): with decimal.context(...): yield We don't want gen's context to leak to the outer scope -- that's one of the reasons why PEP 550 exists. Even if we do this: g = gen() next(g) # the decimal.context won't leak out of gen So a Python user would have a mental model: context set in generators doesn't leak. Not, let's consider a "broken" generator: def gen(): decimal.context(...) yield If we iterate gen() with next(), it still won't leak its context. But if "yield from" has semantics that you want -- "yield from" to be just like function call -- then calling yield from gen() will corrupt the context of the caller. I simply want consistency. It's easier for everybody to say that generators never leaked their context changes to the outer scope, rather than saying that "generators can sometimes leak their context". Yury