Use of the "is" statement

Christian Heimes lists at cheimes.de
Fri Jun 27 12:26:45 EDT 2008


Joel Corbin wrote:
> Thank you Gary, Cédric, Christian. When *would *one use "is"?

As I said:
The "is" statement should only be used when you want to check of
something is exactly and identical to None like "a is None" or "b is not
None". For everything else you should use == or !=. There are some other
use cases for "is", too. But in general you shouldn't use "is".

> Cédric... the problem I was having was purely an issue of comparison.... "if
> file.tell() is 0L" was returning False. Strangely enough, "if file.tell() is
> 0" returns True in the right cases. I assume this is related to the None
> case?

Both usages of "is" are wrong. The second case works by accident,
because Python optimized small ints. The int object 0 and some more
objects are a singletons.

Ask yourself if you are interested if f.tell() returns exactly the same
0 object ("is") or a number that is equal to 0 ("==").

Christian




More information about the Python-list mailing list