For example
x = 5
the value 5 is stored in the memory. Does variable name x, reference and unique identity(id() function) are also stored somewhere in memory. How does it work?
For example
x = 5
the value 5 is stored in the memory. Does variable name x, reference and unique identity(id() function) are also stored somewhere in memory. How does it work?
It is an implementation detail.
For CPython (the common one...), actually even the 5
itself doesn't take any additional space! read here (ints from -5
to 256
live in a pre-allocated array).
Also in CPython, the identity returned by id
is simply the address in memory of the variable, so it is also doesn't take any additional memory to "save".
As for variable themselves, they most probably do take some space in some symbol table that has to remember their binding.