On 13/09/2007, <b class="gmail_sendername">Terry Carroll</b> &lt;<a href="mailto:carroll@tjc.com">carroll@tjc.com</a>&gt; wrote:<div><span class="gmail_quote"></span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On Thu, 13 Sep 2007, Orest Kozyar wrote:<br><br>&gt; Given a variable x that can either be None or a tuple of two floats [i.e.<br>&gt; (0.32, 4.2)], which syntax is considered most appropriate under Python<br>&gt; coding standards?
<br>&gt;<br>&gt; if x and x[0] &gt; 0:<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pass<br>&gt;<br>&gt; =====OR=====<br>&gt;<br>&gt; if x:<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if x[0] &gt; 0:<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pass<br><br>I would like either one if instead of &quot;if x&quot; you used &quot;if x is not None&quot;;
<br>that seems a lot easier to me to read.&nbsp;&nbsp;It&#39;s a bit jarring to see the same<br>variable used in one expression as both a boolean and a list/tuple.<br><br>Besides, suppose somehow x got set to zero.&nbsp;&nbsp;It would pass without error,
<br>something you wouldn&#39;t want to have happen.&nbsp;&nbsp;Even if you&#39;ve set things up<br>so that it couldn&#39;t happen, it&#39;s not obvious from looking at this code<br>that it couldn&#39;t happen.<br><br>If you really want to test for x being non-None, test for x being
<br>non-None.</blockquote><div><br>The problem is what if it&#39;s an empty list or tuple? It would pass but have not value<br>whereas if x would work fine. <br></div><br></div><br>