<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:#954F72;
        text-decoration:underline;}
span.gmail-
        {mso-style-name:gmail-;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style></head><body lang=EN-US link=blue vlink="#954F72"><div class=WordSection1><p class=MsoNormal>Nick: “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:<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>    with decimal.localcontext() as ctx:<o:p></o:p></p><p class=MsoNormal>        ctc.prex = 30<o:p></o:p></p><p class=MsoNormal>        for i in gen():<br>           pass<br><br>    g = gen()<o:p></o:p></p><p class=MsoNormal>    with decimal.localcontext() as ctx:<o:p></o:p></p><p class=MsoNormal>        ctc.prex = 30<o:p></o:p></p><p class=MsoNormal>        for i in g:<br>          pass”</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>I’m following this discussion from a distance, but cared enough about this point to chime in without even reading what comes later in the thread. (Hopefully it’s not twenty people making the same point…)</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>I HATE this example! Looking solely at the code we can see, you are refactoring a function call from inside an *<b>explicit</b>* context manager to outside of it, and assuming the behavior will not change. There’s *<b>absolutely no</b>* logical or semantic reason that these should be equivalent, especially given the obvious alternative of leaving the call within the explicit context. Even moving the function call before the setattr can’t be assumed to not change its behavior – how is moving it outside a with block ever supposed to be safe?</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>I appreciate the desire to be able to take currently working code using one construct and have it continue working with a different construct, but the burden should be on that library and not the runtime. By that I mean that the parts of decimal that set and read the context should do the extra work to maintain compatibility (e.g. through a globally mutable structure using context variables as a slightly more fine-grained key than thread ID) rather than forcing an otherwise straightforward core runtime feature to jump through hoops to accommodate it.</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>New users of this functionality very likely won’t assume that TLS is the semantic equivalent, especially when all the examples and naming make it sound like context managers are more related. (I predict people will expect this to behave more like unstated/implicit function arguments and be captured at the same time as other arguments are, but can’t really back that up except with gut-feel. It's certainly a feature that I want for myself more than I want another spelling for TLS…)</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Top-posted from my Windows phone</p><p class=MsoNormal><o:p> </o:p></p><div style='mso-element:para-border-div;border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in'><p class=MsoNormal style='border:none;padding:0in'><b>From: </b><a href="mailto:ncoghlan@gmail.com">Nick Coghlan</a><br><b>Sent: </b>Tuesday, October 10, 2017 5:35<br><b>To: </b><a href="mailto:guido@python.org">Guido van Rossum</a><br><b>Cc: </b><a href="mailto:python-ideas@python.org">Python-Ideas</a><br><b>Subject: </b>Re: [Python-ideas] PEP draft: context variables</p></div><p class=MsoNormal><o:p> </o:p></p><div><div><div><p class=MsoNormal>On 10 October 2017 at 01:24, Guido van Rossum <<a href="mailto:guido@python.org" target="_blank">guido@python.org</a>> wrote:</p><blockquote style='border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in'><div><div><div><p class=MsoNormal><span class=gmail->On Sun, Oct 8, 2017 at 11:46 PM, Nick Coghlan <<a href="mailto:ncoghlan@gmail.com" target="_blank">ncoghlan@gmail.com</a>> wrote:<o:p></o:p></span></p><blockquote style='border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in'><div><div><div><p class=MsoNormal>On 8 October 2017 at 08:40, Koos Zevenhoven <<a href="mailto:k7hoven@gmail.com" target="_blank">k7hoven@gmail.com</a>> wrote:</p><blockquote style='border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in'><div><div><div><div><div><p class=MsoNormal><span style='font-family:"Cambria Math",serif'>​​</span><span style='font-family:"Courier New"'>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. </span><span style='font-family:"Cambria Math",serif'>​​</span><span style='font-family:"Courier New"'><o:p></o:p></span></p></div></div></div></div></div></blockquote><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>We didn't forget it, we just don't think it's very useful.<br clear=all></p></div></div></div></div></blockquote><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>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.</p></div></div></div></div></blockquote><div><p class=MsoNormal><o:p> </o:p></p></div><p class=MsoNormal>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.</p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>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:</p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>    with decimal.localcontext() as ctx:</p></div><div><p class=MsoNormal>        ctc.prex = 30</p></div><div><p class=MsoNormal>        for i in gen():<br>           pass<br><br>    g = gen()</p><div><p class=MsoNormal>    with decimal.localcontext() as ctx:</p></div><div><p class=MsoNormal>        ctc.prex = 30</p></div><p class=MsoNormal>        for i in g:<br>          pass</p></div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>The easiest way to maintain that equivalence is to say that even though preventing state changes leaking *out* of generators is considered a desirable change, we see preventing them leaking *in* as a gratuitous backwards compatibility break.</p></div><div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>This does mean that *neither* form is semantically equivalent to eager extraction of the generator values before the decimal context is changed, but that's the status quo, and we don't have a compelling justification for changing it.</p></div><div><p class=MsoNormal><o:p> </o:p></p></div></div><div><p class=MsoNormal>If folks subsequently decide that they *do* want "capture on creation" or "capture on first iteration" semantics for their generators, those are easy enough to add as wrappers on top of the initial thread-local-compatible base by using the same building blocks as are being added to help event loops manage context snapshots for coroutine execution.</p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>Cheers,</p></div><div><p class=MsoNormal>Nick.</p></div><div><p class=MsoNormal><br>-- </p></div></div><p class=MsoNormal>Nick Coghlan   |   <a href="mailto:ncoghlan@gmail.com" target="_blank">ncoghlan@gmail.com</a>   |   Brisbane, Australia</p><p class=MsoNormal><o:p> </o:p></p></div></body></html>