<br><br><div class="gmail_quote">On Mon, Oct 5, 2009 at 9:28 PM, Sander Sweers <span dir="ltr"><<a href="mailto:sander.sweers@gmail.com">sander.sweers@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
Hi Tutors,<br>
<br>
I am going through someone's python script and I am seeing a lot of the<br>
following boolean checks.<br>
<br>
if not s == ""<br>
<br>
if not n == 0<br>
<br>
if b == True<br>
<br>
if not b == True<br>
<br>
etc..<br>
<br>
All of these can be written without the == notation like "if n", "if s"<br></blockquote><div><br></div><div>No, they cannot. Some of them can be, others cannot.</div><div>if b == True can be written as if b.</div>
<div><br></div><div>However,</div><div>if not n == 0 can be written as if n != 0 but NOT as if n.</div><div>The reason why is that 0 is not equivalent to False even though it evaluates to False.</div><div>So </div><div>if not n:</div>
<div>would be true for n = 0 and for n = "" and for n = None</div><div>but</div><div>if n != 0:</div><div>would be true for n = "" and n = None but not n = 0.</div><div><br></div><div>The same is true for </div>
<div><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; ">if not s == ""</span></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Now in this case where it is only used as boolean checks which would be<br>
the most pythonic way if writing these checks?<br></blockquote><div>If you're sure they're boolean checks, "if n:" or "if not n:" is usually how I see it written.</div></div>Whoever wrote your code probably thinks he knows the types of the variables beforehand so he's just making assumptions as to whether a variable is an int / string / etc. So you can probably safely assume that they're boolean checks, but I take no responsibility if you break something :)