<div dir="ltr"><br><br><div class="gmail_quote"><div dir="ltr">On Sat, May 12, 2018 at 11:48 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 Sat, May 12, 2018 at 11:04:45PM -0400, Neil Girdhar wrote:<br>
> Another benefit of given compared with := that I just thought of is this.<br>
> Suppose you have a generator like<br>
> <br>
> (expression(f(x), y, z)<br>
> for x in xs<br>
> for y in ys(x)<br>
> for z in zs(y))<br>
> <br>
> With given notation you can optimize:<br>
> <br>
> (expression(f_x, y, z)<br>
> for x in xs<br>
> given f_x = f(x)<br>
> for y in ys(x)<br>
> for z in zs(y))<br>
><br>
> whereas with :=, you can't.<br>
<br>
Is that legal syntax? You're splitting the "given" expression by <br>
sticking the for clause in the middle of it. I don't think that will be <br>
legal. It would be trying to split an ternary if:<br>
<br>
(true_expr for x in xs if condition else false_expr for y in ys)<br>
<br>
(true_expr if condition for x in xs else false_expr for y in ys)<br>
<br>
<br></blockquote><div><br></div><div>You're right that it's a different proposal, but I thought it would be a natural extension to this proposal since at the start of this discussion someone mentioned how this could be accomplished with something like</div><div><br></div><div>(expression</div><div>for x in xs</div><div>for f_x in [f(x)])</div><div><br></div><div>The regular given proposal is an extension of (I think) "expr" into something like</div><div><br></div><div>expr ["given" test annassign]</div><div><br></div><div>I think a natural extension of that is to add it to "testlist_comp" and "dictorsetmaker" so that</div><div><br></div><div>given name = value</div><div><br></div><div>can be used a cleaner synonym of something like</div><div><br></div><div>for name in [value]</div><div><br></div><div> But your'e right, it's a different proposal and I'm getting ahead of things.</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
But whether legal or not, Neil, you went on at length about how <br>
professionals don't write code like this and such overly dense <br>
comprehensions are only fit for competitions.<br>
<br>
Now you want your cake and to eat it too: <br>
<br>
"given is better, because it doesn't allow the awful<br>
unreadable code that := gives; oh, and it's also better, <br>
because it allows *this* awful unreadable code that :=<br>
doesn't allow"<br>
<br>
<br>
In any case, of course you can write this with := binding expression. <br>
You just shouldn't do it:<br>
<br>
(expression(f_x, y, z) for x in xs <br>
for y in ys(x)<br>
for z in zs(y) <br>
if (f_x := f(x)) or True)<br>
)<br>
<br>
<br>
That's fine for mucking about, but I wouldn't do it for serious code. <br>
Replacing the colon with "given f_x" doesn't change that.<br>
<br></blockquote><div><br></div><div>My thought is that if each component is simple enough *and* if each component can be reasoned about independently of the others, then it's fine. The issue I had with the := code you presented was that it was impossible to reason about the components independently from the whole. </div><div><br></div><div>Ultimately, this is one of feelings. I think every one of us has a unique set of experiences, and those experiences led us to having different programming aesthetics. I used to write code one way, and then after lots of code reviews, my experience has made me write code a different way.</div><div><br></div><div>Best,</div><div><br></div><div>Neil</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<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/CFuqwmE8s-E/unsubscribe" rel="noreferrer" target="_blank">https://groups.google.com/d/topic/python-ideas/CFuqwmE8s-E/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>