Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a variable the name, the value, or the memory location?

I've been learning Python for a few months, and also know very little C, and I was wondering if anyone could clear this doubt up for me:

Is a variable the name, the value, or the memory location?

For example:

x = 5

Is the variable x, the value of x, or the location of x in memory?

I'm looking for a clear explanation of what a variable is. I've already looked at Wikipedia's page on variables and this question, but neither were too clear to me. If this is a duplicate question, a link to the right answer would be great.

Thanks!

like image 482
Impossibility Avatar asked Nov 01 '13 04:11

Impossibility


People also ask

Is location a variable?

Because location is also a variable it thus assumes a dominant position in that it is the only variable which is not allowed to overlap. Along with many other procedures in geography, quantitative methods have been developed for regionalization.

What is a variable in computer memory?

In computer programming, a variable is an abstract storage location paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value; or in simpler terms, a variable is a named container for a particular set of bits or type of data (like integer, float, string ...

What is the difference between variable name and value?

In computer programming, a variable or scalar is a storage location paired with an associated symbolic name (an identifier), which contains some known or unknown quantity of information referred to as a value.

Do variables store memory locations?

A variable is a named entity that has a memory location (which can vary over runtime, depending on what type of variable) and that spans one or more memory addresses.


3 Answers

There is a space somewhere in a memory where the values are stored. These memory is then located through address which is usually represented by hexadecimal number, or sometimes binary number. So, these numbers are hard to remember for programmer, so they used the term variable to refer to that memory location in a easy way, or you can say higher abstraction. So, x is just a name that refers to some location in the memory which contains value of 5. That's all!

like image 66
SujitS Avatar answered Nov 19 '22 00:11

SujitS


There are several things going on with the statement x=5:

  1. an int object with the value of 5 is created (or found if it already exists);
  2. the name x is created (or disassociated with the last object 'x' labeled);
  3. the reference count to the new (or found) int object is increased by 1;
  4. the name x is associated with the object with the value '5' created (or found).

Since int objects are immutable, they may be interned for efficiency. String objects are more likely to be interned.

Here are some examples:

>>> x=5    # discussed
>>> id(x)  # the 'id' which in cPython is the memory address.
140246146681256
>>> y=x    # now two names, 'x' and 'y' associated with that object
>>> id(y)  
140246146681256     # same object
>>> z=5    # no guaranteed, likely the same object referred to by 'x' and 'y'
>>> id(z)
140246146681256     # id is the same! The object labeled 'x' was found and labeled 'z'
>>> del x           # ref count to object '140246146681256' decreased by 1
>>> del y           # same
>>> z
5
>>> id(z)
140246146681256    # same object but the names ''x' and 'y' no longer label it

The best way to think of variables names, like 'x' in x=5 is as a label.

It is possible to have objects with no label, such as the 5 individual string objects created in this list comprehension:

>>> li=[str(x) for x in range(5)]  
>>> li
['0', '1', '2', '3', '4']

You can then create objects that match the value of those same 5 objects independently:

>>> li2=list('012345')    # longer list; completely different construction
>>> li2
['0', '1', '2', '3', '4', '5']    

You can get their individual memory addresses (in cPython) or a unique id address:

>>> [id(x) for x in li]
[4373138488, 4372558792, 4372696960, 4373139288, 4373139368]
>>> [id(x) for x in li2]
[4373138488, 4372558792, 4372696960, 4373139288, 4373139368,  4372696720]  

Note that the two independently created lists of anonymous object are the same (for the first 5 in this case). I purposely used strings because they are more likely to be interred...

So think of it this way: two different processes are happening with x=5:

  1. the object is created (or found if it is immutable, interned and exists) and
  2. the object is labeled.
like image 21
dawg Avatar answered Nov 19 '22 00:11

dawg


A variable is a mapping of a name to associated storage and serves as a way to refer to that storage location in your program. When you assign to a variable and perform operations on it, you're populating and altering that storage. When you reassign a variable, you can point it to a different storage location -- you are remapping that name.

like image 1
Christian Ternus Avatar answered Nov 18 '22 23:11

Christian Ternus