0

According to the tutorial, https://docs.python.org/3/reference/datamodel.html#

Every object has an identity, a type and a value. An object’s identity never changes once it has been created; you may think of it as the object’s address in memory. The ‘is’ operator compares the identity of two objects; the id() function returns an integer representing its identity.

CPython implementation detail: For CPython, id(x) is the memory address where x is stored.

However, when I check the id of two different variables, the address are the same. They look only the same values to me, how to interpret the CPython implementation here that they have same memory address but not interfere with each other's modication?

>>> a=1
>>> b=1
>>> a is b
True
>>> id(a)
4475991184
>>> id(b)
4475991184
>>> a=2
>>> id(a)
4475991216
>>> a=1
>>> id(a)
4475991184

I changed variable "a" and changed back, its address is changed back to original one again.

Community
  • 1
  • 1
jtcloud
  • 521
  • 1
  • 4
  • 18
  • Variables do not have IDs. *Objects* have IDs. It sounds like you could use a [quick guide](https://nedbatchelder.com/text/names.html) to the relationship between variables and objects in Python. – user2357112 May 15 '20 at 19:30
  • id is not address location. For smaller integers it is same. see this https://stackoverflow.com/questions/306313/is-operator-behaves-unexpectedly-with-integers – Bhanu Tez May 15 '20 at 19:33

0 Answers0