Showing content from http://mail.python.org/pipermail/python-dev/attachments/20171124/18e84150/attachment-0001.html below:
<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 23 November 2017 at 23:04, Ivan Levkivskyi <span dir="ltr"><<a href="mailto:levkivskyi@gmail.com" target="_blank">levkivskyi@gmail.com</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 class="gmail_extra">I don't see why this particular case qualifies for such a radical measure as an exception to syntactic rules,</div><div class="gmail_extra">instead of just fixing it (sorry Nick :-)</div></div></blockquote><div><br></div>I've posted in more detail about this to the issue tracker, but the argument here is: because making it behave differently from the way it does now while still hiding the loop iteration variable potentially requires even more radical revisions to the lexical scoping rules :)</div><div class="gmail_quote"><br></div><div class="gmail_quote">If somebody can come up with a clever trick to allow yield inside a comprehension to jump levels in a relatively intuitive way, that would actually be genuinely cool, but the lexical scoping rules mean it's trickier than it sounds.</div><div class="gmail_quote"><br></div><div class="gmail_quote">Now that I frame the question that way, though, I'm also remembering that we didn't have "yield from" yet when I wrote the current comprehension implementation, and given that, it may be as simple as having an explicit yield expression in a comprehension imply delegation to a subgenerator.</div><div class="gmail_quote"><br></div><div class="gmail_quote">If we went down that path, then a list comprehension like the following:</div><div class="gmail_quote"><br></div><div class="gmail_quote">   results = [(yield future) for future in list_of_futures]</div><div class="gmail_quote"><br></div><div class="gmail_quote">might be compiled as being equivalent to:</div><div class="gmail_quote"><br></div><div class="gmail_quote">   def __listcomp_generator(iterable):</div><div class="gmail_quote">       result = []</div><div class="gmail_quote">       for future in iterable:</div><div class="gmail_quote">           result.append((yield future))</div><div class="gmail_quote">       return result<br></div><div class="gmail_quote">       <br></div><div class="gmail_quote"><div class="gmail_quote">   results = yield from _listcomp_generator(list_of_futures)</div><div class="gmail_quote"><br></div><div class="gmail_quote">The only difference between the current comprehension code and this idea is "an explicit yield expression in a comprehension implies the use of 'yield from' when calling the nested function".<br></div><div class="gmail_quote"><br></div><div class="gmail_quote">For generator expressions, the adjustment would need to be slightly different: for those, we'd either need to prohibit yield expressions, or else say that if there's an explicit yield expression present anywhere, then we drop the otherwise implied yield expression.<br></div><div class="gmail_quote"><br></div></div><div class="gmail_quote">If we went down the latter path, then:</div><div class="gmail_quote">   <br><div class="gmail_quote">   gen = ((yield future) for future in list_of_futures)</div><div class="gmail_quote"><br></div><div class="gmail_quote">and:</div><div class="gmail_quote"><br></div><div class="gmail_quote"><div class="gmail_quote">   gen = (future for future in list_of_futures)</div><div class="gmail_quote"><br></div><div class="gmail_quote">would be two different ways of writing the same thing.</div><div class="gmail_quote"><br></div><div class="gmail_quote">The pay-off for allowing it would be that you could write things like:</div><div class="gmail_quote"><br></div><div class="gmail_quote"><div class="gmail_quote">   gen = (f(yield future) for future in list_of_futures)</div><div class="gmail_quote"><br></div></div><div class="gmail_quote">as a shorthand equivalent to:</div><div class="gmail_quote"><br></div><div class="gmail_quote">   def gen(list_of_futures=list_of_futures):</div><div class="gmail_quote">       for future in list_of_futures:</div><div class="gmail_quote">           f(yield future)<br></div></div></div></div><div class="gmail_extra"><br></div><div class="gmail_extra">(Right now, you instead get "yield f(yield future)" as the innermost statement, which probably isn't what you wanted)</div><div class="gmail_extra"><br></div><div class="gmail_extra">Cheers,</div><div class="gmail_extra">Nick.<br clear="all"></div><div class="gmail_extra"><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>
RetroSearch is an open source project built by @garambo
| Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4