0

I am declaring a list and checking the reference of the elements in the list. I am able to understand that the "Peter" is stored at 140489131208016, "Bruce" is stored at 140489131207736 and so on and the reference is stored in the list by what is the value that is printed when I do print(id(names)) what does that mean or point to? I searched along a lot of articles, but no clear explanation was provided.

In 'C' if arr = [11, 22, 24], arr will hold the address of the number 11, is that any way related to python?

Python code:

names = ["Peter", "Bruce", "Tony"]

print (id(names))
print (id(names[0]))
print (id(names[1]))
print (id(names[2]))

O/P:

140489122463816
140489131208016
140489131207736
140489131206056
quamrana
  • 37,849
  • 12
  • 53
  • 71
Paul
  • 331
  • 2
  • 7
  • I removed the C tag as the question is not about C. Anyway... your description of how things work in C isn't strictly correct. – Support Ukraine Jun 30 '21 at 08:22
  • Unless you are specifically asking about how to solve a cross-version compatibility problem (in which case your question should obviously describe that problem) you should not mix the [tag:python-2.7] and [tag:python-3.x] tags. I have removed them both for the time being. – tripleee Jun 30 '21 at 08:23
  • `names` points to the list object, list object can hold any number of object – Epsi95 Jun 30 '21 at 08:24
  • 1
    Mandatory link to [Ned Batchelder](https://nedbatchelder.com/text/names.html) – quamrana Jun 30 '21 at 08:26
  • Does this answer your question? [Are python variables pointers? or else what are they?](https://stackoverflow.com/questions/13530998/are-python-variables-pointers-or-else-what-are-they) – MisterMiyagi Jun 30 '21 at 08:28
  • @ThunderCoder in the same link you posted it says 'CPython implementation detail: This is the address of the object in memory.' – Ron Serruya Jun 30 '21 at 08:29
  • @RonSerruya, sorry, missed that part... – Thunder Coder Jun 30 '21 at 08:29
  • Note that asking about ``id`` in terms of memory or execution model is largely pointless, unless you focus on a tight case in some specific implementation. The Python language spec has no concept of memory and references and whatnot. The ``id`` does return the memory location *in the CPython implementation*, but that's entirely incidental – for example, the PyPy implementation pulls ``id``s out of thin air in some cases (e.g. ``float``s). – MisterMiyagi Jun 30 '21 at 08:33

1 Answers1

1

The Python list is not just a memory address, but an object of its own, roughly like a struct in C. Perhaps this crude graphic can help you.

names --> 140489122463816 list
            +--> [0] --> 140489131208016 string "Peter"
            +--> [1] --> 140489131207736 string "Bruce"
            +--> [2] --> 140489131206056 string "Tony"

The members of the list are separate objects outside the list, and each object has some opaque internal structure which is not directly visible to the outside. You can guess roughly what some of these hidden parts are by observing the attributes and methods of these objects; for example, imagine how Python is able to tell you that something is in fact a list.

tripleee
  • 175,061
  • 34
  • 275
  • 318