[Tutor] if n == 0 vs if not n

Wayne srilyk at gmail.com
Tue Oct 6 15:04:11 CEST 2009


On Mon, Oct 5, 2009 at 3:37 PM, Sander Sweers <sander.sweers at gmail.com>wrote:

> Thanks Wesly/Vern for the replies.
>
> On Mon, 2009-10-05 at 21:56 +0200, Luke Paireepinart wrote:
> >         if not n == 0
> >
> > if b == True can be written as if b.
> >
> >
> > However,
> > if not n == 0 can be written as if n != 0 but NOT as if n.
> > The reason why is that 0 is not equivalent to False even though it
> > evaluates to False.
> > So
> > if not n:
> > would be true for n = 0 and for n = "" and for n = None
> > but
> > if n != 0:
> > would be true for n = "" and n = None but not n = 0.
>
> Ah, have not thought about this one. In this case it checks the return
> code of a subprocess command which if not zero means build failure. I am
> leaving these as they are because in my opinion "if not returncode == 0"
> shows clearer what is going on than "if returncode".
>

If it's checking the returncode against a value, Vern makes a good point:

if returncode != 0 makes a whole lot more sense than "if not returncode ==
0"

Though when dealing with an integer return code, doesn't it make more sense
to use the "is" operator?

if returncode is 0:
    #do something

if returncode is not 0:
    #do something

-Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091006/8c4a4385/attachment.htm>


More information about the Tutor mailing list