0

We say that in Python, every object that is created is given a number that uniquely identifies it.

When we let a point to 500 and obtain the id of both a and 500:

a=500
id(a), id(500)
(140733559789896, 140733559789992)

does this mean that the ID of the reference a (140733559789896) "points" to object 500 like a points to 500?

And why is 140733559789992 not the id of the object 500?

Cleptus
  • 3,446
  • 4
  • 28
  • 34
  • Somewhat, it means that 500 is not interned (or whatever it called in Python) and as result 500 stored in `a` is different from any other 500... Try the same with `5`. (Should be duplicate... someone will find it shortly) – Alexei Levenkov Sep 26 '19 at 02:15
  • 2
    Possible duplicate of [Confused about Python’s id()](https://stackoverflow.com/questions/33801444/confused-about-python-s-id) – Alec Sep 26 '19 at 02:21
  • **python doesn't have pointers**. In any case, why is this surprising to you? You've merely created two different objects – juanpa.arrivillaga Sep 26 '19 at 06:09

2 Answers2

0

id(500) creates a new object in memory and assigns an id to it. This is not the same object as a, even if they share the same value.

Imagine we have this piece of code:

a = 500
b = 500

Do we expect id(a) == id(b)? Of course not – they're different objects.

Alec
  • 8,529
  • 8
  • 37
  • 63
  • not always true, try a=10, b=10, id(a)==id(b) .. https://www.journaldev.com/22925/python-id - short but interesting – Derek Eden Sep 26 '19 at 02:35
0

does this mean that the ID of the reference a (140733559789896) "points" to object 500 like a points to 500?

According to the docs for id, it's the address of the object 500 in memory.

And why is 140733559789992 not the id of the object 500?

It is. The 500 in the line a = 500 and the 500 you pass to id(500) in line 2 are different objects, hence the different identities.

kosayoda
  • 400
  • 1
  • 6