0

I learned that id(1) always same because of interning mechanism.

>>> id(1)
14710976
>>> id(1)
14710976
>>> 5
>>> id(1)
14710976
>>>

But when i try id(2000000000), it will intern too if execute id(2000000000) continuosly, but this id will changed after it execute something else (e.g. give print a number). And the id has been reuse every three times of id changing.

As you can see on the session below, 3 ids, 142989240, 142989252, 142989132:

>>> id(2000000000)
142989240
>>> id(2000000000)
142989240
>>> 5
5
>>> id(2000000000)
142989252
>>> id(2000000000)
142989132
>>> 5
5
>>> id(2000000000)
142989132
>>> 5
5
>>> id(2000000000)
142989240
>>> 5
5
>>> id(2000000000)
142989252
>>> 5
5
>>> id(2000000000)
142989132
>>> 

1st question: Why python reuse the id every 3 times? Is there any concerns of python contributors agree to choose 3, not 2 or 4? In which file in the python source code can i find out the 3 times implementation?

2nd question:

I find out if interned two times

>>> id(2000000000)
161535928
>>> id(2000000000)
161535928
>>> 5
5
>>> id(2000000000)
161535940
>>> 5
5
>>> id(2000000000)
161535820

or interned five times

>>> id(2000000000)
161195960
>>> id(2000000000)
161195960
>>> id(2000000000)
161195960
>>> id(2000000000)
161195960
>>> id(2000000000)
161195960
>>> 5
5
>>> id(2000000000)
161195972
>>> 5
5
>>> id(2000000000)
161195852

, then the next 2 ids, will ALWAYS end with the same single digits. As you can see on the session above, after id(2000000000) interned, both 161535940 and 161535820 end with digit 0. After id(2000000000) interned 5 times, both 161195972 and 161195852 end with digit 2.

But if interned once/three/four/six times, then i always get 3 ids which was ending in different digits. As you can see on the session below, the interpreter session shows that 140613560, 140613476, and 140613572 ending with 0, 6, 2 respectively.

>>> id(2000000000)
140613560
>>> id(2000000000)
140613560
>>> id(2000000000)
140613560
>>> 5
5
>>> id(2000000000)
140613476
>>> 5
5
>>> id(2000000000)
140613572

So my question is, why this constant behavior occur and how it implemented?

3rd question:

Related to 2nd question, if intern two or five times, there are special behavior occur.

>>> id(2000000000)
141371320
>>> id(2000000000)
141371320
>>> 5
5
>>> id(2000000000)
141371332
>>> 5
5
>>> id(2000000000)
141371212
>>> 
>>> id(2000000000)
141371212
>>> 5
5
>>> id(2000000000)
141371236
>>> 5
5
>>> id(2000000000)
141371320

Look at the middle of the session above, i press ENTER and leave empty >>> , and the 3 ids reused (refer to 1st question) is changed ! 141371320 -> 141371332 -> 141371212 become 141371212 -> 141371236 -> 141371320. And i reproduced the step and find out the total is 4 ids candidates in the sequence of 141371320 -> 141371332 -> 141371212 -> 141371236

My question was, why this special behavior exist?

Thomas K
  • 39,200
  • 7
  • 84
  • 86
林果皞
  • 7,539
  • 3
  • 55
  • 70
  • 6
    Whoa, please ease back on the images! You are all dealing with *text* here, please copy and paste that text into your question. Also, you are making your question *way too broad* by asking multiple different questions in one post. – Martijn Pieters Aug 28 '14 at 17:27
  • See [this answer](http://stackoverflow.com/a/3402717/2642204). – BartoszKP Aug 28 '14 at 17:28
  • I will change it to text, but please give me some time, thanks. – 林果皞 Aug 28 '14 at 17:33
  • @林果皞 This question is already on hold, so you will not receive answers on it. – admdrew Aug 28 '14 at 17:34
  • @林果皞 You are allowed to ask several questions though. The 4th one is ... _interesting_. – Sylvain Leroux Aug 28 '14 at 17:36
  • 2
    @SylvainLeroux: there are no separate memory areas. You can simply end up using a memory address that uses an integer number larger than `sys.maxint` and then you get a long int in Python 2. – Martijn Pieters Aug 28 '14 at 17:37
  • And none of the behaviour is 'special', just an emergent property of how the system stores and releases objects as reference counts go up and down. – Martijn Pieters Aug 28 '14 at 17:38
  • @MartijnPieters Oups. I didn't pay close enough attention to the numbers :/ – Sylvain Leroux Aug 28 '14 at 17:43
  • Python does allocate some types of objects in their own "arenas" (chunks of memory), which may explain some of the differences you're seeing. – kindall Aug 28 '14 at 17:44
  • @MartijnPieters already answered my 4th question here, so no point i open new question for 4th. But the first three questions is related to each other and i will be blamed if put it to 3 new questions. Thanks. – 林果皞 Aug 28 '14 at 18:08

0 Answers0