<div dir="ltr"><br><br><div class="gmail_quote"><div dir="ltr">On Wed, May 30, 2018 at 7:54 PM Steven D'Aprano <<a href="mailto:steve@pearwood.info">steve@pearwood.info</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Wed, May 30, 2018 at 01:59:37PM -0400, Neil Girdhar wrote:<br>
<br>
> This example shows additional flexibility:<br>
> <br>
> z = {a: transformed_b<br>
>      for b in bs<br>
>      given transformed_b = transform(b)<br>
>      for a in as_}<br>
<br>
Is that even legal?<br></blockquote><div><br></div><div>In case you missed my earlier reply to you:</div><div><br></div><div><div style="color:rgb(33,33,33);font-size:13px">One addition to the grammar would be to "test" for something like</div><div style="color:rgb(33,33,33);font-size:13px"><br></div><div style="color:rgb(33,33,33);font-size:13px">test: bool_test [comp_given]<br>bool_test: or_test ['if' or_test 'else' test] | lambdef</div><div style="color:rgb(33,33,33);font-size:13px">comp_given: 'given' testlist_star_expr annassign<br></div><div style="color:rgb(33,33,33);font-size:13px"><br></div><div style="color:rgb(33,33,33);font-size:13px">The second would permit the usage in comprehensions:</div><br style="color:rgb(33,33,33);font-size:13px"><span style="color:rgb(33,33,33);font-size:13px">comp_iter: comp_for | comp_if | comp_given</span><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Again, you're putting half of the comprehension in the middle of <br>
the given expression. I believe that "given" expression syntax is:<br>
<br>
    expression given name = another_expression<br>
<br>
it's not a syntactic form that we can split across arbitrary chunks of <br>
code:<br>
<br>
    # surely this won't be legal?<br>
    def method(self, arg, x=spam):<br>
        body<br>
    given spam = expression<br>
<br>
<br>
Comprehension syntax in this case is:<br>
<br>
    {key:expr for b in it1 for a in it2}<br>
<br>
(of course comprehensions can also include more loops and if clauses, <br>
but this example doesn't use those). So you've interleaved part of the <br>
given expression and part of the comprehension:<br>
<br>
    {key: expression COMPRE- given name = another_expression -HENSION}<br>
<br>
<br>
That's the second time you've done that. Neil, if my analysis is <br>
correct, I think you have done us a great service: showing that the <br>
"given" expression syntax really encourages people to generate syntax <br>
errors in their comprehensions. <br></blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
> There is no nice, equivalent := version as far as I can tell.<br>
<br>
Given (pun intended) the fact that you only use transformed_b in a <br>
single place, I don't think it is necessary to use := at all.<br>
<br>
z = {a: transform(b) for b in bs for a in as_}<br>
<br>
But if you really insist:<br>
<br>
# Pointless use of :=<br>
z = {a: (transformed_b := transform(b)) for b in bs for a in as_}<br>
<br></blockquote><div><br></div><div>Those call transform for every a needlessly.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<br>
-- <br>
Steve<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
<br>
-- <br>
<br>
--- <br>
You received this message because you are subscribed to a topic in the Google Groups "python-ideas" group.<br>
To unsubscribe from this topic, visit <a href="https://groups.google.com/d/topic/python-ideas/keaR3FudcwQ/unsubscribe" rel="noreferrer" target="_blank">https://groups.google.com/d/topic/python-ideas/keaR3FudcwQ/unsubscribe</a>.<br>
To unsubscribe from this group and all its topics, send an email to <a href="mailto:python-ideas%2Bunsubscribe@googlegroups.com" target="_blank">python-ideas+unsubscribe@googlegroups.com</a>.<br>
For more options, visit <a href="https://groups.google.com/d/optout" rel="noreferrer" target="_blank">https://groups.google.com/d/optout</a>.<br>
</blockquote></div></div>