[Python-Dev] PEP 550 v4
Greg Ewing
greg.ewing at canterbury.ac.nz
Thu Sep 7 06:37:58 EDT 2017
Yury Selivanov wrote:
> I understand what Koos is
> talking about, but I really don't follow you. Using the
> "with-statements to be skipped" language is very confusing and doesn't
> help to understand you.
If I understand correctly, instead of using a context
manager, your fractions example could be written like
this:
def fractions(precision, x, y):
ctx = decimal.getcontext().copy()
decimal.setcontext(ctx)
ctx.prec = precision
yield MyDecimal(x) / MyDecimal(y)
yield MyDecimal(x) / MyDecimal(y ** 2)
and it would work without leaking changes to the decimal
context, despite the fact that it doesn't use a context
manager or do anything else to explicitly put back the
old context.
Am I right about that?
This is what I mean by "skipping context managers" --
that it's possible in some situations to get by without
using a context manager, by taking advantage of the
implicit local context push that happens whenever a
generator is started up.
Now, there are two possibilities:
1) You take advantage of this, and don't use context
managers in some or all of the places where you don't
need to. You seem to agree that this would be a bad
idea.
2) You ignore it and always use a context manager, in
which case it's not strictly necessary for the implicit
context push to occur, since the relevant context managers
can take care of it.
So there doesn't seem to be any great advantage to the
automatic context push, and it has some disadvantages,
such as yield-from not quite working as expected in
some situations.
Also, it seems that every generator is going to incur
the overhead of allocating a logical_context even when
it doesn't actually change any context vars, which most
generators won't.
--
Greg
More information about the Python-Dev
mailing list