On Tue, Oct 10, 2017 at 5:01 PM, Nick Coghlan <ncoghlan@gmail.com> wrote:
On 10 October 2017 at 22:51, Koos Zevenhoven <k7hoven@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 *are* currently equivalent for lazy iterators (including generators), and both different from the form that uses eager evaluation of the values before the context change.

Where should enters into the picture is by way of PEP 550 saying that they should *remain* equivalent because we don't have an adequately compelling 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 iterator was created. If you want to ensure that isn't the case, you have to force eager evaluation before the context change.

What PEP 550 is proposing is that, by default, *nothing changes*: the lazy iteration in the above will continue to use the updated decimal context by default.

​That's just an arbitrary example. There are many things that *would* change if decimal contexts simply switched from using thread-local storage to using PEP 550. It's not at all obvious which of the changes would be most likely 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 only way to ensure full backwards compatibility, regardless of whether PEP 555 or PEP 550 is used. But I'm sure one could decide otherwise.


​––Koos​


--
+ Koos Zevenhoven + http://twitter.com/k7hoven +