变量是名称、值还是内存位置? [英] Is a variable the name, the value, or the memory location?

查看:37
本文介绍了变量是名称、值还是内存位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经学习 Python 几个月了,而且对 C 知之甚少,我想知道是否有人可以为我解开这个疑问:

变量是名称、值还是内存位置?

例如:

x = 5

是变量x,x的值,还是x在内存中的位置?

我正在寻找关于什么是变量的清晰解释.我已经看过维基百科关于变量的页面这个问题,但我都不太清楚.如果这是一个重复的问题,那么提供正确答案的链接会很棒.

谢谢!

解决方案

声明 x=5 有几个问题:

  1. 一个值为 5 的 int 对象被创建(或者如果它已经存在则找到);
  2. 名称 x 被创建(或与最后一个标记为x"的对象解除关联);
  3. 对新的(或找到的)int 对象的引用计数增加 1;
  4. 名称 x 与创建(或找到)值为5"的对象相关联.

由于 int 对象是不可变的,因此它们可能被实习以提高效率.字符串对象更有可能被实习.

以下是一些示例:

<预><代码>>>>x=5 # 讨论过>>>id(x) # 'id' 在 cPython 中是内存地址.140246146681256>>>y=x # 现在有两个名称,'x' 和 'y' 与该对象相关联>>>编号(y)140246146681256#同一个对象>>>z=5 # 不保证,可能是 'x' 和 'y' 引用的同一个对象>>>id(z)140246146681256#id是一样的!找到标记为x"的对象并标记为z">>>del x #对象'140246146681256'的引用计数减少1>>>del y # 一样>>>z5>>>id(z)140246146681256 # 相同的对象,但名称 ''x' 和 'y' 不再标记它

考虑变量名称(例如 x=5 中的x")的最佳方式是将其作为标签.

可以有没有标签的对象,例如在这个列表推导式中创建的 5 个单独的字符串对象:

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

然后,您可以独立创建与这 5 个对象的值匹配的对象:

<预><代码>>>>li2=list('012345') # 更长的列表;完全不同的构造>>>li2['0', '1', '2', '3', '4', '5']

你可以得到它们各自的内存地址(在 cPython 中)或者一个唯一的 id 地址:

<预><代码>>>>[id(x) for x in li][4373138488、4372558792、4372696960、4373139288、4373139368]>>>[li2 中 x 的 id(x)][4373138488、4372558792、4372696960、4373139288、4373139368、4372696720]

请注意,两个独立创建的匿名对象列表是相同的(在本例中为前 5 个).我故意使用字符串,因为它们更有可能被埋葬......

所以这样想:x=5 发生了两个不同的过程:

  1. 对象被创建(或找到,如果它是不可变的,实习的和存在的)和
  2. 对象已标记.

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!

解决方案

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.

这篇关于变量是名称、值还是内存位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆