Python Forum
Python's id() function output in prompt and script mode - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python's id() function output in prompt and script mode (/thread-14562.html)



Python's id() function output in prompt and script mode - stormshadow - Dec-06-2018

In CPython it is agreed that id's (in effect addresses) of objects with values > 256 should be different.

When using the Python prompt in a terminal this is exactly what happens. Doing the same by running a script with the same Python contents gives identical id's.

Python 3 Prompt
>>> a = 500
>>> b = 500
>>> print(id(a))
139745079475056
>>> print(id(b))
139745079475248
Script test.py
python3 /tmp/test.py
139835321420368
139835321420368

Can anyone shine some light on this please?


RE: Python's id() function output in prompt and script mode - Gribouillis - Dec-06-2018

When you run the code in the interactive interpreter, each statement is compiled separately. On each of the two assignment statements, python creates a int instance, so there are two instances.

If you run the code from the file, all the statements are compiled together and I guess python is smart enough to detect that it only needs to create a single int instance for the two literal constants 500. You could have more information by examining bytecode with the help of the dis module.


RE: Python's id() function output in prompt and script mode - stormshadow - Dec-06-2018

Thanks for the prompt reply and tend to agree with what you say. However isn't this behaviour a bit confusing given that in literature we read that int objects between -5 and 256 have the same ID. Anything above is given different IDs.


RE: Python's id() function output in prompt and script mode - casevh - Dec-07-2018

Quote:Thanks for the prompt reply and tend to agree with what you say. However isn't this behaviour a bit confusing given that in literature we read that int objects between -5 and 256 have the same ID. Anything above is given different IDs.

You shouldn't always believe everything you read. Wink

CPython does not make any guarantees at all that two objects representing the same small integer will have the same id().

Here is an example in Python 3.6 where it isn't true.

>>> a=7778 % 77; a; id(a)
1
94562065865216
>>> b=7777777777777778 % 77777777; b; id(b)
1
94562065865216
>>> c=777777777777777777777778 % 777777777777; c; id(c)
1
140053117334672
The CPython interpreter has an optimization that creates objects representing the integers from -5 to 256. Most internal functions inside the CPython interpreter will (eventually) call a function that checks if a result is one of the cached values and if it is, it just returns another reference to the same object. The check is not required and some functions, % in my example, don't do it.

Python does guarantee that the id() of an object won't change during the lifetime of the object. This is a language requirement and not an implementation detail. Once an object is deleted, the id() may, or may not, be used again. The interactive prompt and a program may have subtle differences in when objects are deleted, or in recognizing common expressions and reusing, or not, the same object.

And I've probably glossed over a few details so you shouldn't believe everything in this answer....

casevh


RE: Python's id() function output in prompt and script mode - stormshadow - Dec-10-2018

Great, thanks for the detailed explanation and yes makes sense.