Skip to content Skip to sidebar Skip to footer

What Happens Behind The Scenes When Python Adds Small Ints?

I was fiddling around with id recently and realized that (c?)Python does something quite sensible: it ensures that small ints always have the same id. >>> a, b, c, d, e =

Solution 1:

You've fallen into a not uncommon trap:

id(2 * x + y) == id(300 + x)

The two expressions 2 * x + y and 300 + x don't have overlapping lifetimes. That means that Python can calculate the left hand side, take its id, and then free the integer before it calculates the right hand side. When CPython frees an integer it puts it on a list of freed integers and then re-uses it for a different integer the next time it needs one. So your ids match even when the result of the calculations are very different:

>>>x, y = 100, 40000>>>id(2 * x + y) == id(300 + x)
True
>>>2 * x + y, 300 + x
(40200, 400)

Solution 2:

Python keeps a pool of int objects in certain numbers. When you create one in that range, you actually get a reference to the pre-existing one. I suspect this is for optimization reasons.

For numbers outside the range of that pool, you appear to get back a new object whenever you try to make one.

$ python
Python 3.2 (r32:88445, Apr 152011, 11:09:05) 
[GCC 4.5.220110127 (prerelease)] on linux2
Type"help", "copyright", "credits"or"license"for more information.
>>> x = 300>>> id(x)
140570345270544>>> id(100+200)
140570372179568>>> id(x*2)
140570345270512>>> id(600)
140570345270576

Source

PyObject* PyInt_FromLong(long ival) Return value: New reference. Create a new integer object with a value of ival.

The current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you actually just get back a reference to the existing object. So it should be possible to change the value of 1. I suspect the behaviour of Python in this case is undefined. :-)

emphasis mine

Solution 3:

AFAIK, id has nothing to do with the size of the parameter. It MUST return a life-time unique identifier, and it CAN return the same result for two different parameters, if they do not exist concurrently.

Post a Comment for "What Happens Behind The Scenes When Python Adds Small Ints?"