Python Forum
Python's id() function output in prompt and script mode
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python's id() function output in prompt and script mode
#1
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?
Reply
#2
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.
Reply
#3
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.
Reply
#4
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
Reply
#5
Great, thanks for the detailed explanation and yes makes sense.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in output of a function akbarza 9 1,094 Sep-29-2023, 11:13 AM
Last Post: snippsat
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,000 Jun-29-2023, 11:57 AM
Last Post: gologica
  How to print the output of a defined function bshoushtarian 4 1,234 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  Open windows cmd prompt and run cmds with python Extra 3 1,401 Jul-14-2022, 06:07 AM
Last Post: Gribouillis
  Real-Time output of server script on a client script. throwaway34 2 2,010 Oct-03-2021, 09:37 AM
Last Post: ibreeden
  output correction using print() function afefDXCTN 3 10,959 Sep-18-2021, 06:57 PM
Last Post: Sky_Mx
  things that work in terminal mode but not in sublime mode alok 4 2,813 Aug-11-2021, 07:02 PM
Last Post: snippsat
  string function doesn't work in script ClockPillow 3 2,331 Jul-13-2021, 02:47 PM
Last Post: deanhystad
  python prints none in function output chairmanme0wme0w 3 2,157 Jul-07-2021, 05:18 PM
Last Post: deanhystad
  Automatic user/password entry on prompt by bash script PBOX_XS4_2001 3 2,726 May-18-2021, 06:42 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020