<div dir="ltr"><br><div class="gmail_extra">On Thu, Feb 13, 2014 at 9:58 PM, Chris Angelico <span dir="ltr"><<a href="mailto:rosuav@gmail.com" target="_blank">rosuav@gmail.com</a>></span> wrote:<br><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Responding to your post in different order to the original.<br>
<div class=""><br>
On Fri, Feb 14, 2014 at 8:59 AM, Ram Rachum <<a href="mailto:ram.rachum@gmail.com">ram.rachum@gmail.com</a>> wrote:<br>
> If you'd like to bind to a variable only a part of the condition, this would<br>
> work too:<br>
><br>
> if x<5 with expensive_computation_0() as x:<br>
> # Do something with x<br>
<br>
</div>Definitely don't like this syntax - while it might be useful to<br>
snapshot part of a condition (I've done it in C plenty of times), this<br>
notation feels clumsy. However...<br>
<div class=""><br></div>
<br></blockquote><div><br></div><div>I agree that a non-clunky way to extract variables from conditions with an operator would be nice. Maybe a better syntax would be:<br><br> if (expensive_computation_0() as x)<5:<br>
# Do something with x<br><br></div><div>And likewise for `while` loops,<br><br> while (expensive_computation_0() as x)<5:<br>
# Do something with x<br></div><div><br><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote">> My suggestion:<br>
><br>
> if expensive_computation_0() as x:<br>
> # Do something with x...<br>
> elif expensive_computation_1() as x:<br>
> # Do something with x...<br>
> elif expensive_computation_2() as x:<br>
> # Do something with x...<br>
<br>
... this simpler form does look reasonable. The "as" part will *only*<br>
come at the end of the expression, and it *always* applies to the<br>
whole expression, so it's fairly clear.<br></blockquote><div><br></div><div>Agreed, this looks reasonable to me.<br><br></div><div>These are special cases of PEP 379, "Adding an Assignment Expression" (<a href="http://www.python.org/dev/peps/pep-0379/">http://www.python.org/dev/peps/pep-0379/</a>) from 2009, which has been withdrawn. Perhaps it would be better received if restricted to if/while conditions.<br>
<br></div><div>Nathan<br></div></div></div></div></div>