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

Kent Johnson kent37 at tds.net
Tue Oct 6 16:12:24 CEST 2009


On Tue, Oct 6, 2009 at 9:04 AM, Wayne <srilyk at gmail.com> wrote:

> 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

No! Equality and identity are different. CPython does cache small
integers but that is an implementation detail that you should not rely
on. For large values you cannot presume that equal numbers are also
identical. For example:

In [1]: a=1000000000

In [2]: b=1000000000

In [3]: a==b
Out[3]: True

In [4]: a is b
Out[4]: False

Kent


More information about the Tutor mailing list