[Tutor] integers are also objects?

Lloyd Hugh Allen lha2@columbia.edu
Mon, 03 Dec 2001 05:21:11 -0500


My understanding is that integers are objects in python. Small integers
(less than 100), though, always have the same id in order to increase
speed, or something.

>>> a=99
>>> a is 99
1
>>> a == 99
1
>>> b=100
>>> b==100
1
>>> b is 100
0

(is compares the id of two objects)

karthik Guru wrote:
> 
> hi all!,
> 
> I'm having a very basic python doubt.
> 
> Are integers objects in python?
> 
> d:\>python
> Python 2.1 (#15, Apr 16 2001, 18:25:49) [MSC 32 bit (Intel)] on win32
> Type "copyright", "credits" or "license" for more information.
> >>> i = 10
> >>> a = 5+5
> >>> id(a)
> 8136016
> >>> id(i)
> 8136016
> 
> In both cases, i and a, the id of the objects happened to be same though i
> created 2 different integer objects.
> so (i == a) obviously works.
> 
> So my question is that does "==" work on contents in case of integers or it
> works only on
> references and python makes sure that integer references with same content
> point to the
> same location, the way it works for strings.
> 
> thanks in advance,
> karthik.
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor