Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

python - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...