12

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!

Community
  • 1
  • 1
Impossibility
  • 954
  • 7
  • 20
  • 2
    In this case, the fact that you don't know C is an advantage, because C and python require a different way of thinking about variables and what they represent. – SethMMorton Nov 01 '13 at 05:42

5 Answers5

13

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.
dawg
  • 98,345
  • 23
  • 131
  • 206
  • Very detailed, thanks! So if I'm understanding correctly, a variable is a link between an object in memory and a name? – Impossibility Nov 01 '13 at 08:03
  • More or less correct. `Variable` is not used as much in Python as it is in C. I think it is better to think of it as a name or label for an object. The object is where it is (cloud, memory, disc, to be generated, etc) – dawg Nov 01 '13 at 16:18
5

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!

SujitS
  • 11,063
  • 3
  • 19
  • 41
1

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.

Christian Ternus
  • 8,406
  • 24
  • 39
1

In Python variables are best considered as names.

In the case of numbers, numbers are immutable so reassigning a variable will always make the variable point to a different number without changing the number.

x = 1
y = x
x += 1 # y is still 1

The difference is clearer with mutable objects, where you can change a value OR make the variable refer to a different value

x = [1,2,3]
y = x
x.append(4) # I mutated x's value, so y is now [1,2,3,4]
x = [] # I made x point to a different list, so y is still [1,2,3,4]

Recommended reading:

http://web.archive.org/web/20180411011411/http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html

http://me.veekun.com/blog/2012/05/23/python-faq-passing/

Free Monica Cellio
  • 2,270
  • 1
  • 16
  • 15
0

First Of All

The formal definition of "Memory Model" is a description of the behavior of multi-threaded data access, see Memory_model.

Data Model

According to your description, what you want to express is actually "Data Model", see Python Data Model.

Let me try to summarize:

  1. In Python, data was represented by object.
  2. Every object has three key concepts: identity, a type and a value.
    1. identity: value never changed to identify an object, use id() function to check the id of an object. In CPython, id is object's memory address.
    2. type: object's type, like int/ string/ list or so. never changed. type() to check.
    3. value: the object's value. For example, for a string object, the value is "some string", and it is immutable.
  3. Variable is just a symbol to refer to the object.

Your Question

what happens when typing: x=5?

  1. an object with type int, value 5 was created(or reused if there already has).
  2. a variable named x refer to the object. The object's reference counter increase 1.

That's all.

wahaha
  • 311
  • 4
  • 3