Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
A question about id(), a funny things happened.
id(2000) # is : 48726928 id(1999+1) #is: 48726976 id(1) : # is: 495335184 id(2-1): # is: 495335184 id(0+1): # is:495335184
this is my confusion: id() function don't support it's parameter to add. but before the function proceed, the add 1999+1 had been execute first. So why does the id() has been changed. So when I close the my python editor, and try again id(1): # is: 505886480 has been changed Because before I think every words in the memory has fixed position, but the appearance is the stuff in the memory maybe don't have fixed position, so it always changed. But is good for that, maybe because I still don't know how does memory work. Could you give me a clear answer for the 2 question.
- added problem .2 is when I re-open editor, same id as changed. Thank you very much.
Chris FreemanTreehouse Moderator 68,179 Points
According to the docs, the value returned by
id() is the objects address in memory. This is an integer which is guaranteed to be unique and constant for this object during its lifetime.
When you use
id() on a expression such as
2-1, it is the address of the result that is returned. So
1999+1 is the same as
2-1 is the same as
1. But why then is
id(1999+1) different from
id(2000)? To speed up execution many programming languages preallocate memory for small numbers. This varies by implementation and OS. For example, on my Ubuntu installation, Python appears to preallocate 0 to 256:
>>> id(0) 10105792 >>> id(5-5) 10105792 >>> id(255) 10113952 >>> id(250+5) 10113952 >>> id(255+1) 10113984 >>> id(256) 10113984
Above 256, Python allocates for each expression independently and dynamically as needed:
>>> id(257) 140577560969104 >>> id(256+1) 140577560340240 # Garbage Collection is where unreferenced variables are cleared from # memory and the memory is freed to be use by other code. # After waiting a bit, I see that some of the addresses have been reused >>> id(255+2) 140577560340240 >>> id(256+1) 140577560340208 >>> id(257) 140577560969104
However, once a value is reference by a variable it will not be garbage collected until that variable is deleted or goes of scope (such as local function variable when the function exits).
>>> num257 = 257 >>> num256 = 256 >>> num255 = 255 >>> id(num257) 140577560969104 >>> id(num255) 10113952 >>> id(num256) 10113984 >>> id(num255+1) 10113984 >>> id(num255+2) 140577560340240 >>> id(num256+1) 140577560340208
So back to question #1: For small numbers the memory location will say the same but for larger results a new address is dynamically chosen.
On Question #2: Each time the editor is restarted it renegotiates for memory from the OS, Its previous memory locations having been freed up for other uses when the editor was closed. This means all
id values will likely change. Though it is not impossible for the same memory to become available, it's just highly unlikely.
Post back if you have more questions.
thank you ever so much for the thorough explanation in the question. Thank you very much, let me know the memory on the computer, I know I have much question in that. So I will step by step, and pick up some books, Is so good when I met some problems, there have a teacher like to help you. Feel so good. Have a good day.