0

like this code: id(1) ,The output will be the memory address: 94591972181024 ,and if i assignment a variable x = 1 and write id(x) == id(1) it return True ...which means that the value of x which is 1, stored in the same memory address as number 1 ...does it mean that every numerical value we store, they have a known place in memory before?

  • 1
    This should help explain it: ["is" operator behaves unexpectedly with integers](https://stackoverflow.com/questions/306313/is-operator-behaves-unexpectedly-with-integers). – sj95126 Oct 17 '22 at 22:20
  • 1
    "does it mean that every numerical value we store, they have a known place in memory before?" No. this simply means that the object being referred to by `x` is *the same object* that is created by the literal expression `1`. – juanpa.arrivillaga Oct 17 '22 at 22:36
  • It is very important to understand, `id` *happens* to return the address of the PyObject header, but that is an implementation detail. It is simply supposed to return an integer that is *unique for the lifetime of the object*. – juanpa.arrivillaga Oct 17 '22 at 22:36
  • Also, it is important to understand that in Python, a variable **is not some space reserved in memory**. Rather, it is a name in some namespace (global, local, etc) that can refer to any object – juanpa.arrivillaga Oct 17 '22 at 22:40
  • 1
    *does it mean that every numerical value we store, they have a known place in memory before?* - How could you store *any* data without knowing where in memory you were storing it? – kaya3 Oct 17 '22 at 23:09
  • Does this answer your question? ["is" operator behaves unexpectedly with integers](https://stackoverflow.com/questions/306313/is-operator-behaves-unexpectedly-with-integers) – tevemadar Feb 25 '23 at 23:49

0 Answers0