[Python-Dev] PEP 550 v4
greg.ewing at canterbury.ac.nz
Thu Sep 7 02:26:03 EDT 2017
Guido van Rossum wrote:
> This feels like a very abstract argument. I have a feeling that context
> state propagating out of a call is used relatively rarely -- it must
> work for cases where you refactor something that changes context inline
> into a utility function (e.g. decimal.setcontext()), but I just can't
> think of a realistic example where coroutines (either of the yield-from
> variety or of the async/def form) would be used for such a utility
Yuri has already found one himself, the __aenter__ and __aexit__
methods of an async context manager.
> A utility function that sets context state but also makes a
> network call just sounds like asking for trouble!
I'm coming from the other direction. It seems to me that it's
not very useful to allow with-statements to be skipped in
certain very restricted circumstances.
The only situation in which you will be able to take advantage
of this is if the context change is being made in a generator
or coroutine, and it is to apply to the whole body of that
generator or coroutine.
If you're in an ordinary function, you'll still have to use
a context manager. If you only want the change to apply to
part of the body, you'll still have to use a context manager.
It would be simpler to just tell people to always use a
context manager, wouldn't it?
More information about the Python-Dev