KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Can someone explain this to me? So I've been playing with the id() command in python and came across this: >>> id('cat') 5181152 >>> a = 'cat' >>> b = 'cat' >>> id(a) 5181152 >>> id(b) 5181152 This makes some sense to me except for one part: The string 'cat' has an address in memory before I assign it to a variable. I probably just don't understand how memory addressing works but can someone explain this to me or at least tell me that I should read up on memory addressing? So that is all well and good but this confused me further: >>> a = a[0:2]+'t' >>> a 'cat' >>> id(a) 39964224 >>> id('cat') 5181152 This struck me as weird because 'cat' is a string with an address of 5181152 but the new a has a different address. So if there are two 'cat' strings in memory why aren't two addresses printed for id('cat') ? My last thought was that the concatenation had something to do with the change in address so I tried this: >>> id(b[0:2]+'t') 39921024 >>> b = b[0:2]+'t' >>> b 'cat' >>> id(b) 40000896 I would have predicted the IDs to be the same but that was not the case. Thoughts?
Tags (comma-separated)
Save Edits
Cancel