negative numbers are not equal...

Christian Heimes lists at cheimes.de
Thu Aug 14 16:42:24 EDT 2008


ariel ledesma wrote:
> i read that 'is' compares if they are really the same object, but i 
> don't that's it because then why does -5 return True?
> of course i could only use == to compare, but still, what am i missing 
> here?

Rule of thumb: Never use 'is' with any kind of string or numeric object. 
'is' is not a comparison operator. It checks for object identity.

You are getting the result because Python optimized small integers. See 
http://svn.python.org/projects/python/trunk/Objects/intobject.c Integers 
between -5 and +256 are singletons as are some other objects like 
strings with one element or empty tuples. You must not rely on the 
optimization.

Christian




More information about the Python-list mailing list