[Python-ideas] PEP draft: context variables
k7hoven at gmail.com
Tue Oct 10 12:21:48 EDT 2017
On Tue, Oct 10, 2017 at 5:46 PM, Yury Selivanov <yselivanov.ml at gmail.com>
> On Tue, Oct 10, 2017 at 10:22 AM, Koos Zevenhoven <k7hoven at gmail.com>
> > On Tue, Oct 10, 2017 at 5:01 PM, Nick Coghlan <ncoghlan at gmail.com>
> >> On 10 October 2017 at 22:51, Koos Zevenhoven <k7hoven at gmail.com> wrote:
> >>> I see no reason why these two should be equivalent.
> >> There is no "should" about it: it's a brute fact that the two forms
> >> currently equivalent for lazy iterators (including generators), and both
> >> different from the form that uses eager evaluation of the values before
> >> context change.
> >> Where should enters into the picture is by way of PEP 550 saying that
> >> should *remain* equivalent because we don't have an adequately
> >> justification for changing the runtime semantics.
> >> That is, given the following code:
> >> itr = make_iter()
> >> with decimal.localcontext() as ctx:
> >> ctc.prex = 30
> >> for i in itr:
> >> pass
> >> Right now, today, in 3.6. the calculations in the iterator will use the
> >> modified decimal context, *not* the context that applied when the
> >> was created. If you want to ensure that isn't the case, you have to
> >> eager evaluation before the context change.
It is not obvious to me if changing the semantics of this is breakage or a
bug fix (as you put it below).
> >> What PEP 550 is proposing is that, by default, *nothing changes*: the
> >> iteration in the above will continue to use the updated decimal context
> >> default.
> > That's just an arbitrary example. There are many things that *would*
> > if decimal contexts simply switched from using thread-local storage to
> > PEP 550. It's not at all obvious which of the changes would be most
> > to cause problems. If I were to choose, I would probably introduce a new
> > context manager which works with PEP 555 semantics, because that's the
> > way to ensure full backwards compatibility, regardless of whether PEP
> 555 or
> > PEP 550 is used. But I'm sure one could decide otherwise.
> Please stop using "many things .. would", "most likely" etc.
I can't explain everything, especially not in a single email. I will use
whatever English words I need. You can also think for yourself––or ask a
> We have
> a very focused discussion here. If you know of any particular issue,
> please demonstrate it with a realistic example. Otherwise, we only
> increase the number of emails and make things harder to track for
I'm not going to (and won't be able to) list all those many use cases. I'd
like to keep this more focused too. I'm sure you are well aware of those
differences. It's not up to me to decide what `decimal` should do.
I'll give you some examples below, if that helps.
> If decimal switches to use PEP 550, there will be no "many things that
> *would* change". The only thing that will change is this:
> def g():
> with decimal_context(...):
> next(g()) # this will no longer leak decimal context to the outer world
You forgot `yield from g()`. See also below.
> I consider the above a bug fix, because nobody in their right mind
> relies on partial iteration of generator expecting that some of it's
> internal code would affect your code indirectly.
People use generators for all kinds of things. See below.
> The only such case
> is contextlib.contextmanager, and PEP 550 provides mechanisms to make
> generators "leaky" explicitly.
That's not the only one.
Here's another example:
for c in contexts:
ctx_switcher = context_switcher()
And one more example:
old_ctx = None
first = compute_first_value()
ctx = figure_out_context(first)
old_ctx = decimal.getcontext()
yield from first_things_first()
yield from the_bulk_of_things()
yield from last_but_not_least()
all_things = list(make_things())
+ Koos Zevenhoven + http://twitter.com/k7hoven +
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas