<div dir="ltr"><br><br><div class="gmail_quote"><div dir="ltr">On Sun, May 13, 2018 at 10:56 AM 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 Sun, May 13, 2018 at 12:10:08AM -0400, Neil Girdhar wrote:<br>
<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>
Actually, as you admitted, you can't, since this isn't part of <br>
the proposed syntax or semantics. But let's put that aside.<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>
Except of course you can:<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>
[Neil]<br>
> My thought is that if each component is simple enough *and* if each<br>
> component can be reasoned about independently of the others, then it's<br>
> fine.  The issue I had with the := code you presented was that it was<br>
> impossible to reason about the components independently from the whole.<br>
<br>
I said it was rubbish code which I wouldn't use for serious work. But <br>
"impossible" to reason about? That's pretty strong, and definite, words <br>
for something which is actually pretty easy to reason about.<br></blockquote><div><br></div><div>I was talking about this snippet you wrote:</div><div><br></div><div><span style="color:rgb(33,33,33);font-size:13px">smooth_signal = [(average := (1-decay)*average + decay*x) for x in signal]</span></div><div><br></div><div>Not impossible to reason about, but not pretty either.</div><div>  </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<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></blockquote><div><br></div><div>Not that it matters, but this doesn't work in general since this relies on f_x being evaluable to bool.  You can always just write "for f_x in [f(x)]".  This use of given was more of an aside.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I'm pretty sure you can reason about "expression(f_x, y, z)" on its own. <br>
After all, the above code was (apart from the := line) your own example. <br>
If you can't even reason about your own examples, you're in a bad place.<br>
<br>
I think you can reason about the three for-loops "for x in xs" etc on <br>
their own. Again, they were your idea in the first place.<br>
<br>
I expect you can reason about "if <clause> or True" on its own. It's a <br>
pretty basic Python technique, to call <clause> for its side-effect <br>
without caring about its return value.<br>
<br>
Not something I would use for serious work, but this is YOUR example, <br>
not mine, so if you hate this generator expression, you were the one who <br>
suggested it.<br>
<br>
Only the <clause> is new or different from ordinary Python code that <br>
works now:<br>
<br>
    f_x := f(x)<br>
<br>
The semantics of := are pretty damn simple (at least from the high-level <br>
overview without worrying about precedence or possible scoping issues):<br>
<br>
    - evaluate f(x)<br>
    - assign that value to f_x<br>
    - return the value of f_x<br>
<br>
which is EXACTLY THE SAME SEMANTICS OF "f_x given f_x = f(x)":<br>
<br>
    - evaluate f(x)<br>
    - assign that value to f_x<br>
    - return the value of f_x<br>
<br>
And you would reason about them the same way.<br>
<br>
<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/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>