[Python-Dev] PEP 550 v4

Greg Ewing greg.ewing at canterbury.ac.nz
Wed Sep 6 03:07:12 EDT 2017


Yury Selivanov wrote:
> Greg, have you seen this new section:
> https://www.python.org/dev/peps/pep-0550/#should-yield-from-leak-context-changes

That section seems to be addressing the idea of a generator
behaving differently depending on whether you use yield-from
on it.

I never suggested that, and I'm still not suggesting it.

> The bottomline is that it's easier to
> reason about context when it's guaranteed that context changes are
> always isolated in generators no matter what.

I don't see a lot of value in trying to automagically
isolate changes to global state *only* in generators.

Under PEP 550, if you want to e.g. change the decimal
context temporarily in a non-generator function, you're
still going to have to protect those changes using a
with-statement or something equivalent. I don't see
why the same thing shouldn't apply to generators.

It seems to me that it will be *more* confusing to give
generators this magical ability to avoid with-statements.

> This will have some performance implications and make the API way more
> complex.

I can't see how it would have any significant effect on
performance. The implementation would be very similar to
what's currently described in the PEP. You'll have to
elaborate on how you think it would be less efficient.

As for complexity, push_local_context() and push_local_context()
would be considered low-level primitives that you
wouldn't often use directly. Most of the time they
would be hidden inside context managers.

You could even have a context manager just for applying
them:

    with new_local_context():
       # go nuts with context vars here

> But I'm not convinced yet that real-life code needs the
> semantics you want.

And I'm not convinced that it needs as much magic as you want.

> If you write code that uses 'with' statements consistently, you will
> never even know that context changes are isolated in generators.

But if you write code that uses context managers consistently,
and those context managers know about and handle local
contexts properly, generators don't *need* to isolate their
context automatically.

-- 
Greg



More information about the Python-Dev mailing list