negative numbers are not equal...

Dan Lenski dlenski at gmail.com
Fri Aug 15 09:56:41 EDT 2008


On Thu, 14 Aug 2008 17:18:55 -0300, ariel ledesma wrote:

> hello guys
> 
> i just ran into this when comparing negative numbers, they start
> returning False from -6 down, but only when comparing with 'is'
> 
>  >>> m = -5
>  >>> a = -5
>  >>> m is a
> True
>  >>> m = -6
>  >>> a = -6
>  >>> m is a
> False
>  >>> m == a
> True
> 
> 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? thanks in
> advance

They also return False for positive numbers > 256.  Try this:
 >>> print [x for x in range(-10,260) if x is not x+1-1]
[-10, -9, -8, -7, -6, 257, 258, 259]

There is a good explanation for this: the "is" operator checks for object 
identity, not equality.  Basically "a is m" asks, does the variable name 
"a" reference the same memory location as the variable name "m"?

For integers in the range of -5<=x<=256, Python pre-caches all of these 
values, and whenever a variable takes on one of those values, it uses the 
cached value.  This is an optimization: it prevents the need to allocate 
a new Python object for these very very common small integer values.

For integer literals outside this range, a new Python object gets created 
when they are assigned to variables, so a=500 followed by m=500 will 
create new objects.

The "is" operator just shows the effect of this caching.  It's 
unimportant for real code since you never care whether two numeric 
variables refer to the same object (only important for complex data 
structures where their storage may overlap)... only whether they are 
equal or not.

Dan Lenski

(PS- The small integer pre-caching is described somewhere in the C API 
docs.)




More information about the Python-list mailing list