[Python-Dev] PEP 550 v4
yselivanov.ml at gmail.com
Mon Aug 28 19:16:33 EDT 2017
On Mon, Aug 28, 2017 at 6:56 PM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Yury Selivanov wrote:
>> I saying that the following should not work:
>> def nested_gen():
>> 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:
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()
# the decimal.context won't leak out of gen
So a Python user would have a mental model: context set in generators
Not, let's consider a "broken" generator:
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".
More information about the Python-Dev