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

Sander Sweers sander.sweers at gmail.com
Mon Oct 5 22:37:21 CEST 2009


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".

> 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 :)

When I make the changes and send in a patch it will be reviewed. So any
siliness I come up with will be shot down quickly ;-)

Thanks
Sander 



More information about the Tutor mailing list