<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 15 October 2017 at 14:53, M.-A. Lemburg <span dir="ltr"><<a href="mailto:mal@egenix.com" target="_blank">mal@egenix.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 15.10.2017 06:39, Nick Coghlan wrote:<br>
> On 15 October 2017 at 05:47, Paul Moore <<a href="mailto:p.f.moore@gmail.com">p.f.moore@gmail.com</a><br>
</span><span class="">> <mailto:<a href="mailto:p.f.moore@gmail.com">p.f.moore@gmail.com</a>>> wrote:<br>
><br>
> On 14 October 2017 at 17:50, Nick Coghlan <<a href="mailto:ncoghlan@gmail.com">ncoghlan@gmail.com</a><br>
</span><div><div class="h5">> <mailto:<a href="mailto:ncoghlan@gmail.com">ncoghlan@gmail.com</a>>> wrote:<br>
> > If you capture the context eagerly, then there are fewer opportunities to<br>
> > get materially different values from "data = list(iterable)" and "data =<br>
> > iter(context_capturing_<wbr>iterable)".<br>
> ><br>
> > While that's a valid intent for folks to want to be able to express, I<br>
> > personally think it would be more clearly requested via an expression like<br>
> > "data = iter_in_context(iterable)" rather than having it be implicit in the<br>
> > way generators work (especially since having eager context capture be<br>
> > generator-only behaviour would create an odd discrepancy between generators<br>
> > and other iterators like those in itertools).<br>
><br>
> OK. I understand the point here - but I'm not sure I see the practical<br>
> use case for iter_in_context. When would something like that be used?<br>
><br>
><br>
> Suppose you have some existing code that looks like this:<br>
><br>
> results = [calculate_result(a, b) for a, b in data]<br>
><br>
> If calculate_result is context dependent in some way (e.g. a & b might<br>
> be decimal values), then eager evaluation of "calculate_result(a, b)"<br>
> will use the context that's in effect on this line for every result.<br>
><br>
> Now, suppose you want to change the code to use lazy evaluation, so that<br>
> you don't need to bother calculating any results you don't actually use:<br>
><br>
> results = (calculate_result(a, b) for a, b in data)<br>
><br>
> In a PEP 550 world, this refactoring now has a side-effect that goes<br>
> beyond simply delaying the calculation: since "calculate_result(a, b)"<br>
> is no longer executed immediately, it will default to using whatever<br>
> execution context is in effect when it actually does get executed, *not*<br>
> the one that's in effect on this line.<br>
><br>
> A context capturing helper for iterators would let you decide whether or<br>
> not that's what you actually wanted by instead writing:<br>
><br>
> results = iter_in_context(calculate_<wbr>result(a, b) for a, b in data)<br>
><br>
> Here, "iter_in_context" would indicate explicitly to the reader that<br>
> whenever another item is taken from this iterator, the execution context<br>
> is going to be temporarily reset back to the way it was on this line.<br>
> And since it would be a protocol based iterator-in-iterator-out<br>
> function, you could wrap it around *any* iterator, not just<br>
> generator-iterator objects.<br>
<br>
</div></div>I have a hard time seeing the advantage of having a default<br>
where the context at the time of execution is dependent on<br>
where it happens rather than where it's defined.<br></blockquote><div><br></div><div>The underlying rationale is that the generator form should continue to be as close as we can reasonably make it to being pure syntactic sugar for the iterator form:</div><div><br></div><div> class ResultsIterator:</div><div> def __init__(self, data):</div><div> self._itr = iter(data)</div><div> def __next__(self):</div><div> return calculate_result(next(self._itr))<br></div><div><br></div><div> results = _ResultsIterator(data)<br></div><div><br></div>The logical context adjustments in PEP 550 then serve to make using a with statement around a yield expression in a generator closer in meaning to using one around a return statement in a __next__ method implementation.<br></div><div class="gmail_quote"><div></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
IMO, the default should be to use the context where the line<br>
was defined in the code, since that matches the intuitive<br>
way of writing and defining code.<br></blockquote><div><br></div><div>This would introduce a major behavioural discrepancy between generators and iterators.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
The behavior of also deferring the context to time of<br>
execution should be the non-standard form to not break<br>
this intuition, otherwise debugging will be a pain and<br>
writing fully working code would be really hard in the<br>
face of changing contexts (e.g. say decimal rounding<br>
changes in different parts of the code).<br></blockquote><div><br></div><div>No, it really wouldn't, since "the execution context is the context that's active when the code is executed" is relatively easy to understand based entirely on the way functions, methods, and other forms of delayed execution work (including iterators).</div><div><br></div><div>"The execution context is the context that's active when the code is executed, *unless* the code is in a generator, in which case, it's the context that was active when the generator-iterator was instantiated" is harder to follow.<br></div><div><br></div><div>Cheers,</div><div>Nick.<br></div></div><br>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">Nick Coghlan | <a href="mailto:ncoghlan@gmail.com" target="_blank">ncoghlan@gmail.com</a> | Brisbane, Australia</div>
</div></div>