Understanding memory location of Python variables

Alister alister.ware at ntlworld.com
Sat Jun 16 15:16:37 EDT 2018


On Sat, 16 Jun 2018 13:19:04 -0400, Joel Goldstick wrote:

> On Sat, Jun 16, 2018 at 12:38 PM,  <ip.bcrs at gmail.com> wrote:
>> Hi everyone,
>>
>> I'm intrigued by the output of the following code, which was totally
>> contrary to my expectations. Can someone tell me what is happening?
>>
>>>>> myName = "Kevin"
>>>>> id(myName)
>> 47406848
>>>>> id(myName[0])
>> 36308576
>>>>> id(myName[1])
>> 2476000
>>
>> I expected myName[0] to be located at the same memory location as the
>> myName variable itself. I also expected myName[1] to be located
>> immediately after myName[0].
>> --
>> https://mail.python.org/mailman/listinfo/python-list
> 
> Others can probably give a more complete explanation, but small numbers,
> and apparently letters are cached since they are so common.

also ID is not necessarily a memory location (at least not according to 
the language specification)
the standard cpython implementation does user the memory location for an 
object's ID but this is an implementation detail
 
if you are tying to make use of ID in any way to manipulate computer 
memory your program is fundamentaly broken



-- 
Can you MAIL a BEAN CAKE?



More information about the Python-list mailing list