Comparisons and singletons

Felipe Almeida Lessa felipe.lessa at gmail.com
Sat Mar 25 21:24:32 CET 2006


Em Sáb, 2006-03-25 às 09:11 -0800, Ziga Seilnacht escreveu:
> Python has a special internal list of integers in which it caches
> numbers smaller than 1000 (I'm not sure that the number is correct),
> but that is an implementation detail and you should not rely on it.

By testing:
>>> a = 10
>>> b = 10
>>> a is b
True
>>> a = 100
>>> b = 100
>>> a is b
False
>>> a = 50
>>> b = 50
>>> a is b
True
>>> a = 70
>>> b = 70
>>> a is b
True
>>> a = 99
>>> b = 99
>>> a is b
True

And to the other side:

>>> a = -10
>>> b = -10
>>> a is b
False
>>> a = -5
>>> b = -5
>>> a is b
True
>>> a = -6
>>> b = -6
>>> a is b
False

And then, when looking to Python 2.4's code[1]:
"""
#ifndef NSMALLPOSINTS
#define NSMALLPOSINTS		100
#endif
#ifndef NSMALLNEGINTS
#define NSMALLNEGINTS		5
#endif
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
/* References to small integers are saved in this array so that they
   can be shared.
   The integers that are saved are those in the range
   -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
*/
static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
#endif
"""

However, as stated before, don't rely on these numbers. The trunk[2] defines now 256, not 99, as the biggest integer shared.

[1]
http://svn.python.org/projects/python/tags/release24-fork/Objects/intobject.c 
[2] http://svn.python.org/projects/python/trunk/Objects/intobject.c

HTH,

-- 
Felipe.




More information about the Python-list mailing list