Say I have a function that is <div>def test():</div><div>    return sometimes_false</div><div><br></div><div>if test() as x:</div><div>    print "Its not false"</div><div># what happens to x now? is it a NameError or is it None<br>
<br><div class="gmail_quote">On Sat, Jan 21, 2012 at 10:23 PM, Eike Hein <span dir="ltr"><<a href="mailto:sho@eikehein.com">sho@eikehein.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Hello,<br>
<br>
this is very spur of the moment, and so I apologize if<br>
I am missing the obvious reasons for why this would be<br>
horribly dumb.<br>
<br>
I sometimes find myself annoyed that I cannot access the<br>
result of an expression in a conditional statememt.<br>
<br>
Consider:<br>
<br>
if spam():<br>
   ... frobulate the return value of spam() ...<br>
else:<br>
   ... value too low to frobulate ...<br>
<br>
In order to frobulate spam(), either need to call it<br>
again in the indented block, or preassign:<br>
<br>
x = spam()<br>
<br>
if x:<br>
    ... frobulate x ...<br>
<br>
The first option is usually inefficient, and the second<br>
feels ugly to me because something relevant to a block is<br>
happening outside it and its header.<br>
<br>
Instead, I'd love to be able to:<br>
<br>
if spam() as x:<br>
   ... frobulate x ...<br>
<br>
Further, there are two reasons this seems a particularly<br>
good fit in Python:<br>
- There is a _syntactic_ precedent, or at least great<br>
  similarity to the 'with' statement.<br>
- Because calls in Python return None if they don't ex-<br>
  plicitly return anything else, there is no "can't assign<br>
  void to x" to worry about.<br>
<br>
I haven't given much thought to scoping, particularly for<br>
else clauses, yet, at least not enough to make up my mind.<br>
<br>
The other question is if this reads good "as English" or<br>
needs to be 'if spam() then as x:', or if that's even an<br>
issue.<br>
<br>
Thoughts?<br>
<br>
<br>
Best regards,<br>
Eike<br>
______________________________<u></u>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/<u></u>mailman/listinfo/python-ideas</a><br>
</blockquote></div><br></div>