<br><br><div class="gmail_quote">On Tue, Mar 30, 2010 at 1:08 PM, John Nagle <span dir="ltr"><<a href="mailto:nagle@animats.com">nagle@animats.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Chris Rebert wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
On Tue, Mar 30, 2010 at 8:40 AM, gentlestone <<a href="mailto:tibor.beck@hotmail.com" target="_blank">tibor.beck@hotmail.com</a>> wrote:<br>
</div><div class="im"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi, how can I write the popular C/JAVA syntax in Python?<br>
<br>
Java example:<br>
return (a==b) ? 'Yes' : 'No'<br>
<br>
My first idea is:<br>
return ('No','Yes')[bool(a==b)]<br>
<br>
Is there a more elegant/common python expression for this?<br>
</blockquote>
<br></div><div class="im">
Yes, Python has ternary operator-like syntax:<br></div><div class="im">
return ('Yes' if a==b else 'No')<br>
<br></div><div class="im">
Note that this requires a recent version of Python.<br>
</div></blockquote>
<br>
Who let the dogs in? That's awful syntax.<br><font color="#888888">
<br>
John Nagle</font><div><div></div><div class="h5"><br>
-- <br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br><div><br></div><div>I wouldn't say that. It reads exactly how one would say it. I prefer this over the ? semantics. Whenever I see that, my mind goes "Does a equal b? If so, return this, otherwise return that". "Return this if a equals b, otherwise return that" is much more direct and declaritive IMHO.<br>
</div>