<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 15 October 2017 at 15:05, Guido van Rossum <span dir="ltr"><<a href="mailto:guido@python.org" target="_blank">guido@python.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><div><div>I would like to reboot this discussion (again). It feels to me we're getting farther and farther from solving any of the problems we might solve.</div><div><br></div><div>I think we need to give up on doing anything about generators; the use cases point in too many conflicting directions. So we should keep the semantics there, and if you don't want your numeric or decimal context to leak out of a generator, don't put `yield` inside `with`. (Yury and Stefan have both remarked that this is not a problem in practice, given that there are no bug reports or StackOverflow questions about this topic.)<br></div></div></div></div></blockquote><div><br></div><div>Let me have another go at building up the PEP 550 generator argument from first principles.</div><div><br></div>The behaviour that PEP 550 says *shouldn't* change is the semantic equivalence of the following code:<div><br></div><div>    # Iterator form<br></div><div>    class ResultsIterator:<div>        def __init__(self, data):</div><div>            self._itr = iter(data)</div><div>        def __next__(self):</div><div>            return calculate_result(next(self._<wbr>itr))<br></div><div><br></div><div>    results = _ResultsIterator(data)<br></div><div><br></div></div><div><div>    # Generator form<br></div>    def _results_gen(data):</div><div>        for item in data:</div><div>            yield calculate_result(item)<br></div><div><br><div>    results = _results_gen(data)</div><div><br></div><div>This *had* been non-controversial until recently, and I still don't understand why folks suddenly decided we should bring it into question by proposing that generators should start implicitly capturing state at creation time just because it's technically possible for them to do so (yes we can implicitly change the way all generators work, but no, we can't implicitly change the way all *iterators* work).<br></div><div><br></div></div><div>The behaviour that PEP 550 thinks *should* change is for the following code to become roughly semantically equivalent, given the constraint that the context manager involved either doesn't manipulate any shared state at all (already supported), or else only manipulates context variables (the new part that PEP 550 adds):<div><br></div></div><div><div>    # Iterator form<br></div><div>    class ResultsIterator:<div>        def __init__(self, data):</div><div>            self._itr = iter(data)</div><div>        def __next__(self):</div><div>            with adjusted_context():<br></div><div>                return calculate_result(next(self._<wbr>itr))<br></div><div><br></div><div>    results = _ResultsIterator(data)<br></div><div><br></div></div><div><div>    # Generator form<br></div>    def _results_gen(data):</div><div>        for item in data:</div><div>            with adjusted_context():<br></div><div>                yield calculate_result(item)<br></div><div><br><div>    results = _results_gen(data)<br></div><div><br></div></div></div><div>Today, while these two forms look like they *should* be comparable, they're not especially close to being semantically equivalent, as there's no mechanism that allows for implicit context reversion at the yield point in the generator form.</div><div><br></div><div>While I think PEP 550 would still be usable without fixing this discrepancy, I'd be thoroughly disappointed if the only reason we decided not to do it was because we couldn't clearly articulate the difference in reasoning between:</div><div><br></div><div>* "Generators currently have no way to reasonably express the equivalent of having a context-dependent return statement inside a with statement in a __next__ method implementation, so let's define one" (aka "context variable changes shouldn't leak out of generators, as that will make them *more* like explicit iterator __next__ methods"); and</div><div>* "Generator functions should otherwise continue to be unsurprising syntactic sugar for objects that implement the regular iterator protocol" (aka "generators shouldn't implicitly capture their creation context, as that would make them *less* like explicit iterator __init__ methods").<br></div><div><br></div>Cheers,</div><div class="gmail_quote">Nick.<br clear="all"></div><br>-- <br><div class="gmail_signature">Nick Coghlan   |   <a href="mailto:ncoghlan@gmail.com" target="_blank">ncoghlan@gmail.com</a>   |   Brisbane, Australia</div>
</div></div>