On Tue, Oct 10, 2017 at 3:34 PM, Nick Coghlan <ncoghlan@gmail.com> wrote:
On 10 October 2017 at 01:24, Guido van Rossum <guido@python.org> wrote:
On Sun, Oct 8, 2017 at 11:46 PM, Nick Coghlan <ncoghlan@gmail.com> wrote:
On 8 October 2017 at 08:40, Koos Zevenhoven <k7hoven@gmail.com> wrote:
​​I do remember Yury mentioning that the first draft of PEP 550 captured something when the generator function was called. I think I started reading the discussions after that had already been removed, so I don't know exactly what it was. But I doubt that it was *exactly* the above, because PEP 550 uses set and get operations instead of "assignment contexts" like PEP 555 (this one) does. ​​

We didn't forget it, we just don't think it's very useful.

I'm not sure I agree on the usefulness. Certainly a lot of the complexity of PEP 550 exists just to cater to Nathaniel's desire to influence what a generator sees via the context of the send()/next() call. I'm still not sure that's worth it. In 550 v1 there's no need for chained lookups.

The compatibility concern is that we want developers of existing libraries to be able to transparently switch from using thread local storage to context local storage, and the way thread locals interact with generators means that decimal (et al) currently use the thread local state at the time when next() is called, *not* when the generator is created.

​If you want to keep those semantics in decimal, then you're already done.​

 
I like Yury's example for this, which is that the following two examples are currently semantically equivalent, and we want to preserve that equivalence:

    with decimal.localcontext() as ctx:
        ctc.prex = 30
        for i in gen():
           pass

    g = gen()
    with decimal.localcontext() as ctx:
        ctc.prex = 30
        for i in g:
          pass



​Generator functions aren't usually called `gen`.

Change that to​:

with decimal.localcontext() as ctx:
    ctc.prex = 30
    for val in values():
        do_stuff_with(val)

​# and​

​vals = values()​

​with decimal.localcontext() as ctx:
    ctc.prex = 30
    for val in vals:
        do_stuff_with(val)

​I see no reason why these two should be equivalent.


––Koos


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