0

It's known that variables don't store values but the addresses to those values instead. That's why when we say x=y and change any of them, the change will affect the other, because they point to the same address in memory.

If the variable names store addresses to where the values are, then where are the actual values stored?

I mean, is there a separate place for storing the addresses and another place for storing the actual values where those addresses point to?

Everything is in the RAM, but how the RAM is organized? and how is there a difference between a place in memory that stores an address and another place that stores a real value?

Ehab
  • 566
  • 6
  • 24
  • 2
    At the address in memory pointed to by the variable – itprorh66 Feb 02 '22 at 15:10
  • 1
    Does this answer your question? [print memory address of Python variable](https://stackoverflow.com/questions/16408472/print-memory-address-of-python-variable) – itprorh66 Feb 02 '22 at 15:10
  • 3
    *"If the variable names store addresses to where the values are, then where are the actual values stored?"* - At those addresses. – Kelly Bundy Feb 02 '22 at 15:10
  • What exactly do you think an address *is*? (Hint: in ordinary English usage, what does that word mean? If I said I had a letter with "an address" written on it, would you be able to tell me where the recipient lives?) – Karl Knechtel Feb 02 '22 at 15:24
  • Your question doesn't make it clear if you're asking how Python, how CPython, how the Operational System, etc. are handling the memory. – Iuri Guilherme Dec 20 '22 at 02:18

1 Answers1

-1

There is a builtin function called id() which will print actual memory addres. But there is an interesting thing for storing actual values.

a = 256
b = 256
a is b  # True
a == b  # True
a = 257
b = 257
a is b  # False
a == b  # True

So as you can see is operator checks if a has same address link as b, and == actualy compares using __eq__. But Python caches integers from -5 to 256 and when you assign them twice, in reality Python just assigns same memory address cached.

sudden_appearance
  • 1,968
  • 1
  • 4
  • 15