Understanding memory location of Python variables

Daniel Montecillo daniel.montecillo at montecillo.ca
Sat Jun 16 22:20:05 EDT 2018


I am also wondering about this behavior. Thank you Chris A for providing the explanation.

On Jun 16, 2018, at 5:45 PM, Chris Angelico <rosuav at gmail.com<mailto:rosuav at gmail.com>> wrote:

On Sun, Jun 17, 2018 at 2:38 AM,  <ip.bcrs at gmail.com<mailto: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].


A string or array in C is allocated in contiguous memory. Python,
however, is not C. What you're looking at is the identities of
objects, not the memory locations of bytes. Everything you're assuming
about C should be discarded.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list